#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