美文网首页
Clang-ns_consumed属性

Clang-ns_consumed属性

作者: 传说中的汽水枪 | 来源:发表于2019-02-26 14:50 被阅读0次

    测试是否支持:ns_consumed

    #if __has_attribute(ns_consumed)
        NSLog(@"ns_consumed");
    #else
        NSLog(@"ns_consumed not");
    #endif
    输出:ns_consumed
    

    测试代码:

    - (void)_foo_have_attribute:(id) __attribute((ns_consumed))x {
        NSLog(@"start count in _foo_have_attribute:%zd", [RXMRCUtil objectRetainCount:x]);
        NSLog(@"Do something");
        NSLog(@"end count in _foo_have_attribute:%zd", [RXMRCUtil objectRetainCount:x]);
    }
    
    - (void)_foo:(id)x {
        NSLog(@"start count _foo_not_attribute:%zd", [RXMRCUtil objectRetainCount:x]);
        NSLog(@"Do something");
        NSLog(@"end count in _foo_not_attribute:%zd", [RXMRCUtil objectRetainCount:x]);
    }
    
    - (void)test {
        RXARCTmpObject *object = [[RXARCTmpObject alloc] init];
        NSLog(@"start count outside method:%zd", [RXMRCUtil objectRetainCount:object]);
        
        [self _foo_have_attribute:object]; // 代码1
    //    [self _foo:object]; // 代码2
    
        NSLog(@"end count outside method:%zd", [RXMRCUtil objectRetainCount:object]);
    }
    

    输出结果:

    start count outside method:1
    start count in _foo_have_attribute:2
    Do something
    end count in _foo_have_attribute:2
    end count outside method:1
    RXARCTmpObject dealloc
    

    当注释代码1,打开代码2的时候,输出的数量结果是一样的
    也就是说在ARC,是否加__attribute((ns_consumed))都是一样的。

    相关文章

      网友评论

          本文标题:Clang-ns_consumed属性

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