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

来源:互联网 发布:vmware 设置网络 编辑:程序博客网 时间:2024/06/05 16:15

/**

 @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 = [UIImageimageNamed: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];

    

    // 显示照片信息

    [selfshowPhotoInfo];  //必须在下面

}


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




/**

 懒加载(延迟加载),通过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 = [[NSBundlemainBundle] pathForResource:@"ImageList"ofType:@"plist"];

       NSLog(@"%@", path);

        

        // OCContentsOfFile,通常需要完整的路径

        _imageList = [NSArrayarrayWithContentsOfFile:path];

       NSLog(@"%@",_imageList);

    }

    return_imageList;

}

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


#pragma mark - 控件的懒加载

// getter方法中,不要再使用self.否则会重复调用getter方法,造成死循环

- (UILabel *)noLabel

{

   if (_noLabel ==nil) {

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

        

       _noLabel = label;

        _noLabel.textAlignment  =NSTextAlignmentCenter;

        [self.viewaddSubview:_noLabel];

    }

    return_noLabel;

}






0 0