---数据处理---懒加载
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;
}
@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;
}
相关文章推荐
- HDU1863
- data race check with valgrind
- JS模版引擎实现原理
- android获取屏幕宽高
- JUnit4使用
- Java中获取完整的url
- Raspberrypi 2 树莓派2代 拨号华为E398设备
- MediaWiki 用起来还是比较麻烦的
- clock()与time()区别
- hdoj 1689 Just a Hook 【线段树区间更新求和】
- 对实体完整性和参照完整性的认识
- DMA的基本概念
- NSIndexSet基本用法
- windows下wavelib的配置和使用
- 蓝懿 iOS 技术交流和心得分享 11.25
- HDU 3652 B-number
- Ceph 单/多节点 安装小结 power by CentOS 6.x
- hdu 1754 I Hate It
- 实习半月之感触
- ReactNativeiOS(二)读书记录 2代码结构