@interface Person : NSObject
// 实例化对象(抽取出类)
@property (nonatomic, strong)NSString *name;
@property (nonatomic, strong)NSString *tel;
@property (nonatomic, strong)NSString *pic;
// 遍历初始化(参数是字典)
- (Person *)initWithDictionary:(NSDictionary *)dic;
+ (Person *)personWithDictionary:(NSDictionary *)dic;
@end
#import "Person.h"
@implementation Person
// 自定义构造器
- (Person *)initWithDictionary:(NSDictionary *)dic
{
if (self = [super init])
{
_name = dic[@"name"];
_tel = dic[@"tel"];
_pic = dic[@"pic"];
}
return self;
}
// 类工厂方法
+ (Person *)personWithDictionary:(NSDictionary *)dic
{
return [[self alloc] initWithDictionary:dic];
}
@end
因为所有的数据都被实例化到一个集合,如果分区的话,每个区取值都是从0开始\
因此这里使用实例化对象是得不到分区以及分区索引的:使用属性都在一个集合
// 初始化动态集合
self.arrPerson = [NSMutableArray array];
// 实例化该plist抽取出类的对象:通过字典赋值
for (NSString *key in self.arrSection)
{
// 遍历字典所有value得到的是集合
for (NSDictionary *dic in self.dicData[key])
{
// 遍历该集合得到的每个人对象的信息(字典) 集合存储对象(对象的存储不仅仅局限于该类的指针)
[self.arrPerson addObject:[Person personWithDictionary:dic]];
}
}
// 测试数据存在
NSLog(@"存储的任意对象key为name = %@", [self.arrPerson[12] name]);
原文:http://www.cnblogs.com/pruple/p/5294044.html