美文网首页
Attributes in Clang

Attributes in Clang

作者: 微末凡尘_ | 来源:发表于2017-03-06 23:50 被阅读182次

    原网址:http://clang.llvm.org/docs/AttributeReference.html#id154

    objc_boxable

    Structs and unions marked with the objc_boxable attribute can be used with the Objective-C boxed expression syntax, @(...).
    Usage: attribute((objc_boxable)). This attribute can only be placed on a declaration of a trivially-copyable struct or union:

    使用@(...)对struct或union进行封装

    typedef struct __attribute__((objc_boxable)) {
        int i;
    }some_struct;
    
    typedef union __attribute__((objc_boxable)) {
        int i;
        float f;
    }some_union;
    
    some_struct s1 = {1};
    some_struct s2;
    
    NSValue *boxed = @(s1); // [NSValue value:&s1 withObjCType:@encode(some_struct)];
    
    [boxed1 getValue:&s2];
    NSLog(@"%zd", s2.i);  // 1
    

    objc_method_family

    Many methods in Objective-C have conventional meanings determined by their selectors. It is sometimes useful to be able to mark a method as having a particular conventional meaning despite not having the right selector, or as not having the conventional meaning that its selector would suggest. For these use cases, we provide an attribute to specifically describe the “method family” that a method belongs to.

    Usage: attribute((objc_method_family(X))), where X is one of none, alloc, copy, init, mutableCopy, or new. This attribute can only be placed at the end of a method declaration:

    如果方法名以alloc, new, copy, mutableCopy开头的函数都会被作为生成新对象的函数对返回对象retainCount自增1.
    如果作为普通对象可以在property后加attribute((objc_method_family(none)))

    @interface ViewController : UIViewController
    
    @property NSString *initFoo; // Error init methods must return a type related to the receiver type
    
    @end
    
    @interface ViewController : UIViewController
    
    @property NSString *initFoo;
    - (NSString *)initFoo __attribute__((objc_method_family(none)));
    
    @end
    

    objc_requires_super

    Some Objective-C classes allow a subclass to override a particular method in a parent class but expect that the overriding method also calls the overridden method in the parent class. For these cases, we provide an attribute to designate that a method requires a “call to super” in the overriding method in the subclass.

    Usage: attribute((objc_requires_super)). This attribute can only be placed at the end of a method declaration:

    子类重写父类函数后必须调用父类函数

    @interface A : NSObject
    
    - (void)foo __attribute__((objc_requires_super));
    
    @end
    
    @implementation A
    
    - (void)foo {
    }
    
    @end
    
    @interface B : A
    @end
    
    @implementation B
    
    - (void)foo {
        
        //Warning  Method possibly missing a [super foo] call
    }
    
    @end
    

    objc_runtime_name

    By default, the Objective-C interface or protocol identifier is used in the metadata name for that object. The objc_runtime_name attribute allows annotated interfaces or protocols to use the specified string argument in the object’s metadata name instead of the default name.

    Usage: attribute((objc_runtime_name("MyLocalName"))). This attribute can only be placed before an @protocol or @interface declaration:

    修改@protocol 或@interface编译时的名字

    __attribute__((objc_runtime_name("MyLocalName")))
    @interface Message : NSObject
    @end
    
    @implementation Message
    @end
    
    NSLog(@"%@", NSStringFromClass([Message class]));  // MyLocalName
    
    

    objc_runtime_visible

    This attribute specifies that the Objective-C class to which it applies is visible to the Objective-C runtime but not to the linker. Classes annotated with this attribute cannot be subclassed and cannot have categories defined for them.

    不能被继承和编写类别

    不知道怎么用= = |||

    objc_subclassing_restricted

    This attribute can be added to an Objective-C @interface declaration to ensure that this class cannot be subclassed.

    不能被继承

    __attribute__((objc_subclassing_restricted))
    @interface A : NSObject
    @end
    
    @interface B : A // Error Cannot subclass a class with objc_subclassing_restricted attribute
    @end
    
    

    消除警告

    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Warc-retain-cycles"
        self.completionBlock = ^ {
            ...
        };
    #pragma clang diagnostic pop
    

    参考

    http://nshipster.cn/pragma/

    相关文章

      网友评论

          本文标题:Attributes in Clang

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