模型

使用字典的坏处

  • 需要写对key和value,编辑器没有智能提示,需要手敲,敲错则则读取不了
  • 敲错了编译器不会you任何警告,造成取错或者设错数据

模型的概念

  • 模型:专门用来存放数据的对象

  • 一般都是一些直接继承自NSObject的纯对象

  • 提供一些属性来存放数据

使用模型的好处

  • 其实就是数据模型,专门用来存放数据的对象,用它来表示数据更专业

  • 使用模型访问属性时,编译器会提供提示,提高编码效率

#import <Foundation/Foundation.h>

@interface Shop : NSObject
@property (nonatomic,strong)NSString *name;
@property (nonatomic,strong)NSString *icon;

模型应该提供一个可以传入字典参数的构造方法
// 一般会同时重写对象和类方法
- (id)initWithDict:(NSDictionary *)dict;

+ (id)shopWithDict:(NSDictionary *)dict;

@end
// 商品索引
    NSInteger index = self.shopsView.subviews.count;
    NSDictionary *dict = self.shops[index];
    // 调用对象方法
    // 屏蔽内部细节,把字典传到内部去接收数据
//    Shop *shop = [[Shop alloc] initWithDict:dict];
    // 调用类方法
    Shop *shop = [Shop shopWithDict:dict];

instancetype和id都能表示任何对象类型

重写构造方法,返回id的坏处

  • 编译器不会检测id的真实类型,编译时不会报错,运行会挂
  • 但是instancetype会检测真实类型,不符合会报警告
  • instancetype只能做返回值,id可以用在数据类型
    id obj = @"wzz";
    

类前缀

  • 建议创建类时,类名加前缀,避免公司开发时同名

一个控件看不见有哪些可能?

  • 宽度或高度其实为0
  • 位置不对(比如负数或超大的数,已经超出屏幕了)
  • hidden == YES
  • alpha <= 0.01
  • 没有设置背景色、没有设置内容
  • 可能是文字颜色和背景色的差别

简单的MVC

C-->M C-->V

  • Controller 控制器,大管家

  • Model 数据模型,数据

  • View 视图,显示

只要给image图片吗传空的值,控制台会打印下面的代码

[UIImage imageNamed:nil];

CUICatalog:Invalid asset name supplied:(null)

加载数据到模型

- (NSArray *)heros
{
    if (_heros == nil) {
        NSString *path = [[NSBundle mainBundle]pathForResource:@"heroes.plist" ofType:nil];
        NSArray *herosArray = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *herosArrayM = [NSMutableArray array];
        for (NSDictionary *dict in herosArray) {
            WZHero *hero = [WZHero heroWithDict:dict];
            [herosArrayM addObject:hero];
        }
        _heros = herosArrayM;
    }
    return _heros;
}

results matching ""

    No results matching ""