笑声不断

黑马程序员-字典转模型知识点总结

由于刚基础,对于字典转模型中很多细节的知识点也知之甚少,所以将学习字典转模型中的相关问题进行了总结,其中都是我自己不太理解的。从加载到各个语句的解析,可能

有出入的地方。

字典转换模型时需要掌握的知识点,具体例子

app加载流程

1> app 从mainBundle中加载Plist

2> 按照plist中的数据数量先确定各个View的大小和位置

3> 不过,类似这样的很多图标,控件很多的 UI 设计,建议不使用故事板,而是使用代码创建,否则后期维护也麻烦。

当使用字典时,要将支持文件中放入plist文件,且拖拽素材到supporting files,需要注意拖拽时勾选的项目的区别:

 

大多数情况,往项目中拖拽素材时,通常选择 Destination,

Folders:选择第一项:创建组,这样 xcode 导航显式的是黄色文件夹,要知道,Xcode中资源素材是分文件夹存放的,但是在Bundle中所有素材所在,都在同一个文件夹下,这样勾选的开发效率很高,但是,不能出现文件重名的情况,会让美工不舒服。特点:可以直接使用[NSBundle mainBundle]作为资源路径,效率高!可以使用[UIImage imageNamed:]加载图像

 

如果选择第二项, 创建文件家的引用:xcode显得是蓝色文件夹,此时资源在Xcode中分文件夹,在Bundle中也分文件夹,因此,可以出现文件重名的情况,特点:需要在[NSBundle mainBundle]的基础上拼接实际的路径,效率较差!不能使用[UIImage imageNamed:]加载图像

NSBundle 的理解和 mainBundle 类方法详解NSBundle mainbundle

作用:产生相关文件的全路径。

Bundle是一个目录,其中包含程序所用的资源,如声音图像等,为了方便能偶提取这些文件,对应的bundle,cocoa提供了NSBundle,该类继承自NSObject。

我们将所有的资源放在一个目录中,就是main bundle,所以必须先通过NSBundle得到main bundle(主目录),在主目录下寻找对应命名的子资源。当我们得到一个NSBundle对象后我们就能访问其中的资源了。

NSBundle *bundle = [NSBundle mainBundle]

NSString *path = [bundle pathForResource:@"app.plist" ofType:nil];

或者:

NSString *file = [[NSBundle mainBundle] pathForResource:name ofType:nil];

 

userInterfaceEnable 用户交互行为,当设置为YES时,能够同用户交互,当设置为NO时则不行。例如Uiview和Uiimageview,一般情况下Uiview原则上来说UIImageView是UIView的子类,那么根据里氏替换原则,任何用UIView的地方都应该可以用一个UIImageView替代。于是我就用一个UIImageView替换MainViewController中的view,用来做背景图。但是,问题出来了,上面放一个UITableView之后(addSubView),这个UITableView只能显示数据,但是不能滚动,如果我imageView和tableView都是addSubView上去的则一切正常,这是因为UIView的userInterfaceEnable为YES,能够和用户交互,处理你滑动屏幕时,进行滚动,但是UIImageView的userInterfaceEnable为NO对你触屏时没有反应的。也就是没办法同你进行交互。

UIImageView 与 UIImage 区别

 

1.UIImageView不支持内部图片平铺(tile)

 

2.资源中的图片要用小写的,模拟器中可能不区分大小写,但在真机中区分.

 

   [UIImage imageNamed:@""]; 在设备中区分大小写

 

3.UIView没有背景图属性,有背景色属性.设置背景图可以用addSubView(backgroundImage);,推荐的是设置背景色。

 

4.[UIImage imageNamed:@""];//它是有缓存特性的

 

  This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

 

  On a device running iOS 4 or later, the behavior is identical if the device’s screen has a scale of1.0. If the screen has a scale of2.0, this method first searches for an image file with the same filename with an@2xsuffix appended to it. For example, if the file’s name isbutton, it first searches forbutton@2x. If it finds a 2x, it loads that image and sets thescale property of the returnedUIImage object to2.0. Otherwise, it loads the unmodified filename and sets thescale property to1.0.

 

   On iOS 4 and later, the name of the file is not required to specify the filename extension. Prior to iOS 4, you must specify the filename extension.

 

    可能在多次操作之后,应用经常发生内存警告从而导致自动退出的问题。定位之后发现是由于[UIImage imageNamed: @""]分配的图像都没有释放引起的。而之前从官方的reference中得到的信息应该是[UIImage imageNamed:@""]分配的图像系统会放到cache里面。而关于cache管理的规则就没有明确的介绍。由此看来[UIImage imageNamed:]只适合与UI界面中小的贴图的读取,而一些比较大的资源文件应该尽量避免使用这个接口。

 

  5.  + (UIImage *)imageWithContentsOfFile:(NSString *)path  //这个方法does not cache the image object.

OC中的readonly,当你在@property中用readonly时只是表示编译器会帮你自动生成getter方法,同时不会自动帮你生成setter方法。既然编译器不给你setter你又不自己去定义setter方法。那你当然无法赋值。所以如果你要改属性的话,需要重写set方法而能通过方法进行属性的改变。

9宫格行数和列数之间算法分析

  1、加载一个UIview,每个宫格对应一个UIview,在UIview的基础上再加载图表(UIimageview)和UIlable和“下载” UIbutton。

  2、计算每个UIview的位置(CGfolat  X和 Y),

UIView *appView = [[UIView alloc] initWithFrame:CGRectMake(x, y, viewW, viewH)];

  3、添加UIview的详细内容(UIimageview)和UIlable和“下载” UIbutton。

  4、处理“下载” UIbutton的点击事件。

字典转模型中涉及的问题:

1、类的理解,字典转模型,相当于不断的生成模型(类)对象,而对象拥有类中的属性,这些属性值就是他们在字典中对应的value。

2、现将plist转换成字典数组,将字典放在数组中,再将数组转为模型数组,该数组就是拥有很多的对象。

在各个函数中涉及的代码有:

在viewController.m文件中,调用封装好的字典转模型方法

- (NSArray *)questions
{
    if (!_questions) {
        _questions = [LFQuestion questions];
    }
    return _questions;
}

在模型.h文件中设置字典中对应的属性值和需要方法声明,一个需要实例化对象的类方法,还有字典实例化对象的成员方法

#import <Foundation/Foundation.h>

@interface LFQuestion : NSObject

@property (nonatomic, copy) NSString *answer;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, strong) NSArray *options;

@property (nonatomic, strong) UIImage *image;

/** 用字典实例化对象的成员方法 */
- (instancetype)initWithDict:(NSDictionary *)dict;
/** 用字典实例化对象的类方法,又称工厂方法 */
+ (instancetype)questionWithDict:(NSDictionary *)dict;

/** 从plist加载对象数组 */
+ (NSArray *)questions;

@end
模型.m方法
#import "LFQuestion.h"

@implementation LFQuestion

+ (instancetype)questionWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}

- (instancetype)initWithDict:(NSDictionary *)dict
{
    self = [super init];
    if (self) {
        self.answer = dict[@"answer"];
        self.icon = dict[@"icon"];
        self.title = dict[@"title"];
        self.options = dict[@"options"];
        
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}


+ (NSArray *)questions
{
    NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"questions.plist" ofType:nil]];
    //将字典转换成字典数组
    NSMutableArray *arrayM = [NSMutableArray array];
    
    for (NSDictionary *dict in array) {
        [arrayM addObject:[LFQuestion questionWithDict:dict]];
    }//涉及类方法的调用
    
    return arrayM;
}
@end

 

posted on 2016-01-13 16:14  笑声不断  阅读(194)  评论(0编辑  收藏  举报

导航