Bean对象封装,数据分离

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface Bean : NSObject


@property (nonatomic,copy) NSString *dec;
@property (nonatomic,copy) NSString *pageCount;
@property (nonatomic,copy) NSString *imageName;
@property (nonatomic,strong) UIImage *image;

-(instancetype)initWithDict:(NSDictionary *) dict;
/**
 *  快速创建对象,赋值对象
 *
 *  @param dict <#dict description#>
 *
 *  @return <#return value description#>
 */
+(instancetype)initBean:(NSDictionary *) dict;
+(NSArray *)appList;
@end



#import "Bean.h"
@implementation Bean
-(UIImage *)image{
    if(_image==nil){
        _image=[UIImage imageNamed:self.imageName];
    }
    return _image;
}
-(instancetype) initWithDict:(NSDictionary *)dict{
    self=[super init];
    if(self){
        //方法1
        //        self.dec=dict[@"dec"];
        //        self.pageCount=dict[@"id"];
        //        self.imageName=dict[@"imageName"];
        //方法2
        //        [self setValue:dict[@"dec"] forKey:@"dec"];
        //        [self setValue:dict[@"pageCount"] forKey:@"pageCount"];
        //        [self setValue:dict[@"imageName"] forKey:@"imageName"];
        //方法3
        [self setValuesForKeysWithDictionary:dict];
        
    }
    return self;
}
+(instancetype)initBean :(NSDictionary *)dict{
    return [[self alloc] initWithDict:dict];
}

//优化代码
+(NSArray *)appList{//懒加载
        NSString *path=  [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];
        //2.获取plist 赋值到数组
        NSArray  *array=[NSArray arrayWithContentsOfFile:path] ;
        //3.创建一个临时???数组
        NSMutableArray *arrayM =[NSMutableArray array];
        //4.遍历
        for (NSDictionary *dict in array) {
            //5.赋值
            // Bean *bean =  [Bean initBean:dict];
            //6.增加到临时???数组
            [arrayM addObject:[Bean initBean:dict]];
        }
    return arrayM;
}
@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值