美文网首页
iOS学习笔记3

iOS学习笔记3

作者: 蠢萌的L君 | 来源:发表于2016-04-08 16:26 被阅读19次

Plist文件

  • 加载Plist文件
    1. 先获取文件路径
    2. 在将Plist文件传入数组或者字典中
    // 获取file在不同机器的文件全部路径
    NSString *file = [[NSBundle mainBundle] pathForResource:@"文件名" ofType:@"格式名"];
    // 如果plist根为array就用array接受,为dictionary就用dictionary接收
    [NSArray arrayWithContentsOfFile:file];

常见问题

  • 项目中某个.m文件无法使用
  • 检查Build Phases -> Compile Sources
  • 项目中某个资源文件(plist,音频等)文件无法使用
  • 检查Build Phases -> Copy Bundle Resources
  • 注意将plist加入工程中,注意文件夹的颜色,黄色会虚拟文件夹,蓝色为真是存在的文件夹

懒加载

  • 节约内存,优化性能
  • 重写get方法
-(NSArray *)shop{
    if (_shop == nil)
    {
        NSString *file = [[NSBundle mainBundle] pathForResource:@"文件名" ofType:@"格式名"];
        // 如果plist根为array就用array接受,为dictionary就用dictionary接收
        _shop = [NSArray arrayWithContentsOfFile:file];
        
    }
    return _shop;
}

模型

  • 什么是模型
    • 专门存放数据的对象
    • 继承NSObject
    • 提供属性存放属性
  • 字典转模型
    • 方法最好放在模型中
- (instancetype)initWihtDict:(NSDictionary *)dict;
+ (instancetype)shopWithDict:(NSDictionary *)dict;
  • 字典转模型过程:
    plist -> 字典 -> 模型

相关文章

网友评论

      本文标题:iOS学习笔记3

      本文链接:https://www.haomeiwen.com/subject/mryvlttx.html