美文网首页MacOS开发 技术集锦iOS&Mac
Declared Properties(声明的属性)

Declared Properties(声明的属性)

作者: ADreamClusive | 来源:发表于2018-08-31 11:20 被阅读0次

    当编译器遇到属性声名(参见Declared Properties in The Objective-C Programming Language)时,它生成与内部类、类别或协议相关联的描述性元数据。您可以使用支持在类或协议上按名称查找属性的函数来访问此元数据,以@encode字符串的形式获取属性类型,并将属性的特征列表复制为C字符串数组。每个类和协议都有声明的属性列表。

    1 属性类型和函数

    Property结构体定义了属性描述符的不透明句柄。

    typedef struct objc_property *Property;
    

    你可以使用class_copyPropertyListprotocol_copyPropertyList函数,分别检索与类(包括加载的类别)和协议相关联的属性数组:

    objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount)
    objc_property_t *protocol_copyPropertyList(Protocol *proto, unsigned int *outCount)
    

    例如,给出如下类声明:

    @interface Lender : NSObject {
        float alone;
    }
    @property float alone;
    @end
    

    你可以使用:

    id LenderClass = objc_getClass("Lender");
    unsigned int outCount;
    objc_property_t *properties = class_copyPropertyList(LenderClass, &outCount);
    

    获取属性列表。

    你可以使用property_getName函数来发现一个属性的名字:

    const char *property_getName(objc_property_t property)
    

    你可以使用class_getPropertyprotocol_getProperty函数,分别在类和协议中获取一个给定的名字的属性的引用:

    objc_property_t class_getProperty(Class cls, const char *name)
    objc_property_t protocol_getProperty(Protocol *proto, const char *name, BOOL isRequiredProperty, BOOL isInstanceProperty)
    

    你可以使用property_getAttributes函数,获取一个属性的名字和@encode类型的字符串。有关编码类型字符串的详细信息,参见Type Encodings;关于该字符串的详细信息,参见Property Type String 和 Property Attribute Description Examples。

    const char *property_getAttributes(objc_property_t property)
    

    把这些放在一块,使用下面的代码,你可以打印与一个类关联的所有属性的列表:

    id LenderClass = objc_getClass("Lender");
    unsigned int outCount, i;
    objc_property_t *properties = class_copyPropertyList(LenderClass, &outCount);
    for (i = 0; i < outCount; i++) {
        objc_property_t property = properties[i];
        fprintf(stdout, "%s %s\n", property_getName(property), property_getAttributes(property));
    }
    

    2 属性类型字符串

    你可以使用property_getAttributes函数发现属性的名字和@encode类型字符串,以及属性的其他特征。字符串以T跟随着@encode类型和逗号开始,以V伴随后备实例变量名称结束。在这些符号之间,属性由以下描述符指定,用逗号分隔:

    表7-1 声明的属性类型编码

    Code Meaning
    R The property is read-only (readonly).
    C The property is a copy of the value last assigned (copy).
    & The property is a reference to the value last assigned (retain).
    N The property is non-atomic (nonatomic).
    G<name> The property defines a custom getter selector name. The name follows the G (for example, GcustomGetter,).
    S<name> The property defines a custom setter selector name. The name follows the S (for example, ScustomSetter:,).
    D The property is dynamic (@dynamic).
    W The property is a weak reference (__weak).
    P The property is eligible for garbage collection.
    t<encoding> Specifies the type using old-style encoding.

    例如, 参见 Property Attribute Description Examples.

    3 属性特征描述实例

    给定这些定义:

    enum FooManChu { FOO, MAN, CHU };
    struct YorkshireTeaStruct { int pot; char lady; };
    typedef struct YorkshireTeaStruct YorkshireTeaStructType;
    union MoneyUnion { float alone; double down; };
    

    下表展示了样本属性声明和由property_getAttributes返回的相应的字符串:

    Property declaration Property description
    @property char charDefault; Tc,VcharDefault
    @property double doubleDefault; Td,VdoubleDefault
    @property enum FooManChu enumDefault; Ti,VenumDefault
    @property float floatDefault; Tf,VfloatDefault
    @property int intDefault; Ti,VintDefault
    @property long longDefault; Tl,VlongDefault
    @property short shortDefault; Ts,VshortDefault
    @property signed signedDefault; Ti,VsignedDefault
    @property struct YorkshireTeaStruct structDefault; T{YorkshireTeaStruct="pot"i"lady"c},VstructDefault
    @property YorkshireTeaStructType typedefDefault; T{YorkshireTeaStruct="pot"i"lady"c},VtypedefDefault
    @property union MoneyUnion unionDefault; T(MoneyUnion="alone"f"down"d),VunionDefault
    @property unsigned unsignedDefault; TI,VunsignedDefault
    @property int (*functionPointerDefault)(char *); T^?,VfunctionPointerDefault
    @property id idDefault;Note: the compiler warns: "no 'assign', 'retain', or 'copy' attribute is specified - 'assign' is assumed" T@,VidDefault
    @property int *intPointer; T^i,VintPointer
    @property void *voidPointerDefault; T^v,VvoidPointerDefault
    @property int intSynthEquals;In the implementation block:@synthesize intSynthEquals=_intSynthEquals; Ti,V_intSynthEquals
    @property(getter=intGetFoo, setter=intSetFoo:) int intSetterGetter; Ti,GintGetFoo,SintSetFoo:,VintSetterGetter
    @property(readonly) int intReadonly; Ti,R,VintReadonly
    @property(getter=isIntReadOnlyGetter, readonly) int intReadonlyGetter; Ti,R,GisIntReadOnlyGetter
    @property(readwrite) int intReadwrite; Ti,VintReadwrite
    @property(assign) int intAssign; Ti,VintAssign
    @property(retain) id idRetain; T@,&,VidRetain
    @property(copy) id idCopy; T@,C,VidCopy
    @property(nonatomic) int intNonatomic; Ti,VintNonatomic
    @property(nonatomic, readonly, copy) id idReadonlyCopyNonatomic; T@,R,C,VidReadonlyCopyNonatomic
    @property(nonatomic, readonly, retain) id idReadonlyRetainNonatomic; T@,R,&,VidReadonlyRetainNonatomic

    相关文章

      网友评论

        本文标题:Declared Properties(声明的属性)

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