• Xcode4.5出现的objectc 新语法 (mark)


    @synthesize 可以不用再写了

    如果在.h文件里有
    <pre class="brush:objc; toolbar: true; auto-links: false;">
    @propery NSObject * aProperty
    </pre>
    那么可以认为 编译器会类似在.m文件里生成
    <pre class="brush:objc; toolbar: true; auto-links: false;">
    @synthesize aProperty = _aProperty
    </pre>

    如果上面的都不认识 就可以认为 以下代码
    <pre class="brush:objc; toolbar: true; auto-links: false;">
    @interface Spouce:NSObject

    @property (strong) NSObject * child

    @end

    @implement Spouce

    @end
    </pre>
    和下面的代码没有太大区别(如果在ARC时代写retain、release。。。。)
    <pre class="brush:objc; toolbar: true; auto-links: false;">
    @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
    </pre>

    @literals

    @除了可以表示NSString对象外。现在还可以表示数字、数组、字典、和表达式
    <pre class="brush:objc; toolbar: true; auto-links: false;">
    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前面了。。
    </pre>
    然后就是这些东西可以互相嵌套,比如
    <pre class="brush:objc; toolbar: true; auto-links: false;">
    NSArray * array = @[@{@"name" : @"Bacon", @"age" : @12}, @{@"name" : @"Dave", @"age" : @14}];
    </pre>

    NSArray 和 NSDictionary 可以使用[]语法了
    现在可以直接写:
    <pre class="brush:objc; toolbar: true; auto-links: false;">
    id firstElement = anArray[0];
    anArray[0] = newValue;
    </pre>
    替代以前的
    <pre class="brush:objc; toolbar: true; auto-links: false;">
    id firstElement = [anArray objectAtIndex:0];
    [anArray replaceObjectAtIndex:0 withObject:newValue];
    </pre>

    <pre class="brush:objc; toolbar: true; auto-links: false;">
    id value = aDictionary[@"key"];
    aDictionary[@"key"] = newValue;
    </pre>
    替代以前的
    <pre class="brush:objc; toolbar: true; auto-links: false;">
    id value = [aDictionary objectForkey:@"key"];
    [aDictionary setObject:newValue forKey:@"key"];
    </pre>


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

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

    <pre class="brush:objc; toolbar: true; auto-links: false;">
    @interface AClass () 

    - (void) privateMethod;

    @end

    @implement AClass

    - (void) privateMethod{
    }

    @end
    </pre>

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

    <pre class="brush:objc; toolbar: true; auto-links: false;">
    @implement AClass

    - (void) privateMethod{
    }

    @end
    </pre>

    如果不知道ClassExtension语法的,
    我只想说,ClassExtension是类似Category语法的东西,在.m文件内添加一个无名的Category的@interface声明,然后就可以在里面写私有方法声明,避免编译器乱提示警告。
    如果不知道Category语法。。。。。。。。。。。。。。。。


  • 相关阅读:
    华为超级应用联合创新计划启动,共同打造极致用户体验
    华为P20无敌拍摄能力开放 如何即刻获得?
    两千万次服务的背后,华为终端开放实验室到底做了什么?
    HUAWEI HiAI亮相华为开发者生态大会 助力应用AI开发实现加速度
    搜狐新闻APP是如何使用HUAWEI DevEco IDE快速集成HUAWEI HiAI Engine
    旅行助手:重新定义旅行
    世界更清晰,搜狐新闻客户端集成HUAWEI HiAI 亮相荣耀Play发布会!
    Android和设置alpha(图像)透明度
    Android应用开发欢迎界面不想显示最上面的LOGO
    聊天页面输入框和发送按钮的布局问题 Android
  • 原文地址:https://www.cnblogs.com/DamonTang/p/2814135.html
Copyright © 2020-2023  润新知