zoukankan      html  css  js  c++  java
  • Xcode4.5出现时的OC新语法

    From:http://www.cocoachina.com/bbs/read.php?tid=107251

     

    @synthesize 可以不用再写了


    如果在.h文件里有
    1
    @propery NSObject * aProperty

    那么可以认为 编译器会类似在.m文件里生成
    1
    @synthesize aProperty = _aProperty


    如果上面的都不认识 就可以认为 以下代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    @interface Spouce:NSObject
     
    @property (strong) NSObject * child
     
    @end
     
    @implement Spouce
     
    @end

    和下面的代码没有太大区别(如果在ARC时代写retain、release。。。。)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    @interface Spouce:NSObject{
        NSObject * _child;
    }
    - (NSObject *) child;
    - (void) setChild:(NSObject *) aChild;
    @end
     
    @implement Spouce
    - (NSObject *) child{
        return _child;
    }
    - (void) setChild:(NSObject *) aChild{
        [aChild retain];
        [_child release];
        _child = aChild;
    }
    @end


    @literals

    @除了可以表示NSString对象外。现在还可以表示数字、数组、字典、和表达式
    1
    2
    3
    4
    5
    6
    7
    8
    NSString * string = @"a string object";
     
    NSNumber * numberFromNumber= @12;
    NSNumber * numberFromExpression= @(20 + 40);
    NSArray * array = @[obj1, obj2]; 
    //注意上面不再需要nil结尾
    NSDictionary * dictionary  = @{@"key1" : value1, @"key2" : value2};
    //上面也不再需要了,而且key在value前面了。。

    然后就是这些东西可以互相嵌套,比如
    1
    NSArray * array = @[@{@"name" : @"Bacon", @"age" : @12}, @{@"name" : @"Dave", @"age" : @14}];


    NSArray 和 NSDictionary 可以使用[]语法了
    现在可以直接写:
    1
    2
    id firstElement = anArray[0];
    anArray[0] = newValue;

    替代以前的
    1
    2
    id firstElement = [anArray objectAtIndex:0];
    [anArray replaceObjectAtIndex:0 withObject:newValue];


    1
    2
    id value = aDictionary[@"key"];
    aDictionary[@"key"] = newValue;

    替代以前的
    1
    2
    id value = [aDictionary objectForkey:@"key"];
    [aDictionary setObject:newValue forKey:@"key"];



    在.m自己用的“私有”消息可以不用ClassExtension表达了。

    想在.m文件里添加自己的一些消息(方法、函数 同义)而不在.h文件里出现,可以在最近的ClassExtension语法里表达如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    @interface AClass ()
     
    - (void) privateMethod;
     
    @end
     
    @implement AClass
     
    - (void) privateMethod{
    }
     
    @end


    现在 可以直接在.m文件里写,而编译器不会出现警告此方法未声明

    1
    2
    3
    4
    5
    6
    @implement AClass
     
    - (void) privateMethod{
    }
     
    @end


    如果不知道ClassExtension语法的,
    我只想说,ClassExtension是类似Category语法的东西,在.m文件内添加一个无名的Category的@interface声明,然后就可以在里面写私有方法声明,避免编译器乱提示警告。
    如果不知道Category语法。。。。。。。。。。。。。。。。
     
    other:http://blog.sina.com.cn/s/blog_a2774bb1010127qj.html
  • 相关阅读:
    Just oj 2018 C语言程序设计竞赛(高级组)D: 四边形面积
    Just Oj 2017C语言程序设计竞赛高级组A: 求近似值(矩阵快速幂)
    HDU 1166 敌兵布阵(线段树/树状数组模板题)
    HDU 1541 STAR(树状数组)
    Just Oj 2017C语言程序设计竞赛高级组E: DATE ALIVE(二分匹配)
    Just Oj 2017C语言程序设计竞赛高级组D: 字符串最大表示(next数组)
    蓝桥杯 历届试题 小计算器
    蓝桥杯练习 十六进制转二进制
    51 nod 1212 无向图最小生成树(Kruckal算法/Prime算法图解)
    51 Nod 1240 莫比乌斯函数
  • 原文地址:https://www.cnblogs.com/superchao8/p/2717791.html
Copyright © 2011-2022 走看看