[OC Runtime編程指南_翻譯]一切揭、介紹
[OC Runtime編程指南_翻譯]二狞甚、運(yùn)行時(shí)版本和平臺(tái)
[OC Runtime編程指南_翻譯]三、與運(yùn)行時(shí)交互
[OC Runtime編程指南_翻譯]四廓旬、消息傳遞
[OC Runtime編程指南_翻譯]五哼审、動(dòng)態(tài)方法解析
[OC Runtime編程指南_翻譯]六、消息轉(zhuǎn)發(fā)
[OC Runtime編程指南_翻譯]七、類(lèi)型編碼
[OC Runtime編程指南_翻譯]八涩盾、聲明屬性注:pdf翻譯文檔百度云下載鏈接十气,密碼:zcs2
當(dāng)編譯器遇到屬性聲明(請(qǐng)參閱 _Objective-C _編程語(yǔ)言中 聲明屬性)時(shí),它會(huì)生成與封閉類(lèi)旁赊、類(lèi)別或協(xié)議相關(guān)聯(lián)的描述性元數(shù)據(jù)桦踊。您可以使用支持在類(lèi)或協(xié)議上按名稱(chēng)查找屬性、以@encode
字符串形式獲取屬性類(lèi)型以及以C字符串?dāng)?shù)組形式復(fù)制屬性屬性列表的函數(shù)來(lái)訪問(wèn)此元數(shù)據(jù)终畅。聲明的屬性列表可用于每個(gè)類(lèi)和協(xié)議
。
屬性類(lèi)型和功能
Property
結(jié)構(gòu)定義屬性描述符的不透明句柄竟闪。
typedef struct objc_property *Property;
可以使用函數(shù)class_copyPropertyList
和protocol_copyPropertyList
分別檢索與類(lèi)
(包括加載的類(lèi)別)和協(xié)議
相關(guān)聯(lián)的屬性數(shù)組
:
objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount)
objc_property_t *protocol_copyPropertyList(Protocol *proto, unsigned int *outCount)
例如离福,給定以下類(lèi)聲明:
@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ù)來(lái)發(fā)現(xiàn)屬性的名稱(chēng)
:
const char *property_getName(objc_property_t property)
可以使用函數(shù)class_getProperty
和protocol_getProperty
分別獲取對(duì)類(lèi)和協(xié)議中給定名稱(chēng)的屬性的引用
:
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ù)來(lái)發(fā)現(xiàn)屬性的名稱(chēng)和@encode
type字符串。有關(guān)編碼類(lèi)型字符串的詳細(xì)信息炼蛤,請(qǐng)參閱 類(lèi)型編碼妖爷;有關(guān)此字符串的詳細(xì)信息,請(qǐng)參閱 Property Type String和 Property Attribute Description Examples理朋。
const char *property_getAttributes(objc_property_t property)
將這些組合在一起絮识,可以使用以下代碼打印與類(lèi)關(guān)聯(lián)的所有屬性的列表:
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));
}
屬性類(lèi)型字符串
可以使用property_getAttributes
函數(shù)來(lái)發(fā)現(xiàn)屬性的名稱(chēng)、@encode
type字符串以及屬性的其他屬性嗽上。
字符串以T
開(kāi)頭次舌,后跟@encode
類(lèi)型和逗號(hào)
,以V
結(jié)尾兽愤,后跟支持實(shí)例變量的名稱(chēng)
彼念。其中,屬性由以下描述符指定浅萧,用逗號(hào)分隔:
Table 7-1 聲明的屬性類(lèi)型編碼
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. |
示例, 請(qǐng)參見(jiàn) Property Attribute Description Examples.
Property Attribute 描述示例
根據(jù)這些定義:
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 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 |