zoukankan      html  css  js  c++  java
  • OC语法相关

    @property 参数

    copy:NSString;

    strong:一般对象

    weak:UI控件

    assign:基本数据类型

    instancetype

    instancetype在类型表示上,跟id一样,可以表示任何对象类型

     

    instancetype只能用在返回值类型上,不能像id一样用在参数类型上

     

    instancetypeid多一个好处:编译器会检测instancetype的真实类型

     

     instancetype:

    instancetype 主要用于在类方法实例化对象时,让编译器主动推断对象的实际类型

     

     以避免使用id,会造成开发中不必要的麻烦,减少出错几率!

     

     instancetype是苹果在iOS7才开始主推的

     

     C++11 auto

     swift语言中,绝大多数类的实例化,都不需要再指定类型

     

     instancetype只能用于返回值使用!!!不能当做参数使用

     

     1 #import <Foundation/Foundation.h>
     2 
     3 @interface HMAppInfo : NSObject
     4 
     5 @property (nonatomic, copy) NSString *name;
     6 @property (nonatomic, copy) NSString *icon;
     7 
     8 /**
     9  @property 
    10  
    11  1. 生成getter方法
    12  2. 生成setter方法
    13  3. 生成带下划线的成员变量(记录属性内容)
    14  
    15  readonly的属性不会生成带下划线的成员变量!
    16  */
    17 @property (nonatomic, strong, readonly) UIImage *image;
    18 
    19 /**
    20  instancetype 主要用于在类方法实例化对象时,让编译器主动推断对象的实际类型
    21  
    22  以避免使用id,会造成开发中不必要的麻烦,减少出错几率!
    23  
    24  instancetype是苹果在iOS7才开始主推的
    25  
    26  C++11 auto
    27  在swift语言中,绝大多数类的实例化,都不需要再指定类型
    28  
    29  instancetype只能用于返回值使用!!!不能当做参数使用
    30  */
    31 
    32 /** 通常在写模型的实例化方法时,以下两个方法,都需要实现 */
    33 /** 使用字典实例化模型 */
    34 - (instancetype)initWithDict:(NSDictionary *)dict;
    35 /** 类方法可以快速实例化一个对象 */
    36 + (instancetype)appInfoWithDict:(NSDictionary *)dict;
    37 
    38 /** 返回所有plist中的数据模型数组 */
    39 + (NSArray *)appList;
    40 
    41 @end
     1 #import "HMAppInfo.h"
     2 
     3 @implementation HMAppInfo
     4 // 合成指令,主动指定属性使用的成员变量名称
     5 @synthesize image = _image;
     6 
     7 /**
     8  使用KVC的注意事项
     9  
    10  1> plist中的键值名称必须与模型中的属性一致
    11  2> 模型中的属性可以不全部出现在plist中
    12  */
    13 - (UIImage *)image
    14 {
    15     if (_image == nil) {
    16         _image = [UIImage imageNamed:self.icon];
    17     }
    18     return _image;
    19 }
    20 
    21 - (instancetype)initWithDict:(NSDictionary *)dict
    22 {
    23     // self 是 对象
    24     self = [super init];
    25     if (self) {
    26         // 用字典给属性赋值,所有与plist键值有关的方法,均在此处!
    27 //        self.name = dict[@"name"];
    28 //        self.icon = dict[@"icon"];
    29         
    30         // KVC - key value coding键值编码
    31         // 是一种间接修改/读取对象属性的一种方法
    32         // KVC 被称为 cocoa 的大招!
    33         // 参数:
    34         // 1. 数值
    35         // 2. 属性名称
    36 //        [self setValue:dict[@"name"] forKeyPath:@"name"];
    37 //        [self setValue:dict[@"icon"] forKeyPath:@"icon"];
    38         // setValuesForKeysWithDictionary本质上就是调用以上两句代码
    39         [self setValuesForKeysWithDictionary:dict];
    40     }
    41     return self;
    42 }
    43 
    44 + (instancetype)appInfoWithDict:(NSDictionary *)dict
    45 {
    46     // self 是 class
    47     return [[self alloc] initWithDict:dict];
    48 }
    49 
    50 + (NSArray *)appList
    51 {
    52     NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil]];
    53     
    54     // 创建一个临时数组
    55     NSMutableArray *arrayM = [NSMutableArray array];
    56     
    57     // 遍历数组,依次转换模型
    58     for (NSDictionary *dict in array) {
    59         [arrayM addObject:[HMAppInfo appInfoWithDict:dict]];
    60     }
    61     
    62     return arrayM;
    63 }
    64 
    65 @end

    // 在成员方法中,如果给self赋值,只能在initXXX方法中进行

    // 语法约定:

    // 1> 所有的方法首字母小写

    // 2> 当单词切换的时候,单词首字母大写(驼峰法)

    // 3> 类名要大写

     

     

  • 相关阅读:
    【动态规划】01背包问题
    【Huffman&&贪心】Fence Repair(POJ 3253)
    【STL学习】priority_queue
    【贪心算法】特殊的密码锁(openjudge8469)
    【贪心+二分】疯牛
    用类模板封装链表
    Qt的QString和C++string之间的转换
    Qt模态对话框和非模态对话框
    常见的交换变量的三种方法
    整理的经典面试题及各种库函数的自己实现
  • 原文地址:https://www.cnblogs.com/liqiantu/p/4388791.html
Copyright © 2011-2022 走看看