您的位置:首页 > 其它

---数据处理---懒加载

2015-11-25 22:28 363 查看
/**

@proerty

1. 创建了getter & setter方法

2. 生成一个带_的成员变量,直接读取成员变量不会经过getter方法&setter方法

strong & weak

* 控件

如果是用Stroyboard拖线,控件用weak

如果用代码创建界面,控件可以用strong

* 自定对象,需要使用strong

* NSString,使用copy

* 数字型的int,使用assign

*/

/** ---------------------------------------------------------------------------*/

/**

@proerty

1. 创建了getter & setter方法

2. 生成一个带_的成员变量,直接读取成员变量不会经过getter方法&setter方法

*/

/** ---------------------------------------------------------------------------*/
问题1.

- (void)showPhotoInfo

{

//
效率不高,每次都会生成数组

//
如何解决?使用属性记录字典数组

// NSDictionary *dict1 = @{@"name": @"biaoqingdi", @"desc": @"表情1"};

// NSDictionary *dict2 = @{@"name": @"bingli", @"desc": @"病例1"};

// NSDictionary *dict3 = @{@"name": @"chiniupa", @"desc": @"吃牛扒1"};

// NSDictionary *dict4 = @{@"name": @"danteng", @"desc": @"蛋疼1"};

// NSDictionary *dict5 = @{@"name": @"wangba", @"desc": @"网吧1"};

// NSArray *array = @[dict1, dict2, dict3, dict4, dict5];

//
设置图像和描述

self.iconImage.image = [UIImage
imageNamed:self.imageList[self.index][@"name"]];

self.descLabel.text =
self.imageList[self.index][@"desc"];

self.rightButton.enabled = (self.index !=
4);

self.leftButton.enabled = (self.index !=
0);

}

/** ---------------------------------------------------------------------------*/

问题2.

// :代码的先后顺序存在依赖

// NSDictionary *dict1 = @{@"name": @"biaoqingdi", @"desc": @"表情1"};

// NSDictionary *dict2 = @{@"name": @"bingli", @"desc": @"病例1"};

// NSDictionary *dict3 = @{@"name": @"chiniupa", @"desc": @"吃牛扒1"};

// NSDictionary *dict4 = @{@"name": @"danteng", @"desc": @"蛋疼1"};

// NSDictionary *dict5 = @{@"name": @"wangba", @"desc": @"网吧1"};

// _imageList = @[dict1, dict2, dict3, dict4, dict5];

//
显示照片信息

[self
showPhotoInfo]; //必须在下面
}

/** ---------------------------------------------------------------------------*/

/**

懒加载(延迟加载),通过getter实现

效果:让对象在最需要的时候才创建!

*/
- (NSArray *)imageList
{

NSLog(@"读取图像信息");

if (_imageList ==
nil) {

NSLog(@"实例化数组");

NSDictionary *dict1 =
@{@"name":
@"biaoqingdi", @"desc":
@"表情1"};

NSDictionary *dict2 =
@{@"name":
@"bingli", @"desc":
@"病例1"};

NSDictionary *dict3 =
@{@"name":
@"chiniupa", @"desc":
@"吃牛扒1"};

NSDictionary *dict4 =
@{@"name":
@"danteng", @"desc":
@"蛋疼1"};

NSDictionary *dict5 =
@{@"name":
@"wangba", @"desc":
@"网吧1"};

_imageList = @[dict1, dict2, dict3, dict4, dict5];
}

return
_imageList;
}

/** ---------------------------------------------------------------------------*/

plist文件

/**

懒加载(延迟加载),通过getter实现

效果:让对象在最需要的时候才创建!

*/
- (NSArray *)imageList
{

NSLog(@"读取图像信息");

if (_imageList ==
nil) {

NSLog(@"实例化数组");

// "包" Bundle [NSBundle mainBundle]编译安装之后对应的程序包

NSString *path = [[NSBundle
mainBundle] pathForResource:@"ImageList"
ofType:@"plist"];

NSLog(@"%@", path);

//
在OC中ContentsOfFile,通常需要完整的路径

_imageList = [NSArray
arrayWithContentsOfFile:path];

NSLog(@"%@",
_imageList);
}

return
_imageList;
}

/** ---------------------------------------------------------------------------*/

#pragma mark - 控件的懒加载

// 在getter方法中,不要再使用self.
否则会重复调用getter方法,造成死循环
- (UILabel *)noLabel
{

if (_noLabel ==
nil) {

UILabel *label = [[UILabel
alloc] initWithFrame:CGRectMake(0,
20, self.view.bounds.size.width,
40)];

_noLabel = label;

_noLabel.textAlignment =
NSTextAlignmentCenter;
[self.view
addSubview:_noLabel];
}

return
_noLabel;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: