運(yùn)行時(shí)聲明屬性

翻譯

聲明屬性

當(dāng)編譯器遇到屬性聲明(see Declared Properties in The Objective-C Programming Language)時(shí),編譯器產(chǎn)生以類、分類、協(xié)議進(jìn)行封裝的描述性元數(shù)據(jù)。你能夠使用函數(shù)來獲取這個(gè)元數(shù)據(jù),該函數(shù)通過類名或者協(xié)議名來支持查看一個(gè)類,從而以@encode字符串的形式來獲取屬性類型,以C字符串?dāng)?shù)組的形式獲取屬性描述列表,聲明屬性的列表是用于每個(gè)類和協(xié)議。

屬性類型及功能

屬性結(jié)構(gòu)體定義了一個(gè)不透明的屬性描述

typedef struct objc_property *Property;

你能使用該函數(shù)class_copyPropertyList和protocol_copyPropertyList以數(shù)組的形式獲取屬性與類(包括加載類)和協(xié)議 分別為:

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函數(shù)去獲取屬性名字

const char *property_getName(objc_property_t property)

你能使用函數(shù)class_getProperty和protocol_getProperty得到一個(gè)引用,通過一個(gè)給定的類名和給定協(xié)議分別:

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函數(shù)去獲取屬性名和以@encode 字符串的編碼類型

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));
}

屬性類型

你能使用property_getAttributes函數(shù)獲取一個(gè)屬性的名稱和以@encode 類型的編碼,和屬性的其他描述。這個(gè)字符串以"T"跟隨@encode類型開始, 并且以"V"跟隨返回實(shí)例變量名字為結(jié)尾。在這兩者之間,指定了一下描述,通過“,”分割:

表7-1 聲明屬性編碼類型

Code 意義
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.

屬性描述

提供這些描述

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

下邊表格展示了屬性描述樣例和通過property_getAttributes返回的相應(yīng)的字符串

屬性聲明 屬性描述
@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 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; T@,VidDefault
@property int *intPointer; T^i,VintPointer
@property void *voidPointerDefault; T^v,VvoidPointerDefault
@property int 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(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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容