单例模式:传值
单一的实例 唯一的对象 全工程中唯一的一个对象
作用是通过单例来保存工程中的多个页面中共享的对象和数据
生命周期和程序的生命周期一样 当程序结束时单例对象释放掉
/*单例类.h**/
继承自NSObject:因为只处理数据,不需要显示,不需要控制传值
#import<Foundation/Foundation.h>
@interface DataHandle : NSObject
+(instancetype)shareDateHandle;
@property(nonatomic,copy)NSString dataTitle;
@property(nonatomic,retain)NSMutableArray allKeysArray;
@end
/**/
/*单例类.m**/
#import “DataHandle.h”
@implementation DataHandle
+(instancetype)shareDateHandle
{
//static声明静态变量
//特点:静态变量存储在静态区(全局区)只能初始化一次
static DataHandle *dataHandle = nil;
if (dataHandle == nil) {
dataHandle = [[DataHandle alloc]init];
[dataHandle setStudentDic];
}
return dataHandle;
}
@end
/*/
/***取出数据**/
-(void)setStudentDic
{
NSString filePath = [[NSBundle mainBundle] pathForResource:@”Students” ofType:@”plist”];
//通过文件路径取出字典
NSMutableDictionary bigDic = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
NSLog(@”bigDic = %@”,bigDic);
self.allStuDic = [NSMutableDictionary dictionary];
for (NSString *key in bigDic) {
NSMutableArray *array = [bigDic objectForKey:key];
NSMutableArray *stuArray = [NSMutableArray array];
for (NSMutableDictionary *dic in array) {
Student *stu = [[Student alloc] init];
// stu.name = [dic objectForKey:@”name”];
//使用KVC间接给属性赋值
[stu setValuesForKeysWithDictionary:dic];
[stuArray addObject:stu];
[stu release];
}
[self.allStuDic setObject:stuArray forKey:key];
}
self.allKeysArray = [NSMutableArray arrayWithArray:self.allStuDic.allKeys];
//数组排序
[self.allKeysArray sortUsingSelector:@selector(compare:)];
}
/**/
`