您的位置:首页 > 移动开发 > IOS开发

iOS runtime 学习之类的属性动态获取(一)

2017-02-24 11:19 337 查看
苹果官方文档()如下:

Property Type String

You can use the property_getAttributes function to discover the name, the @encode type string of a property, and other attributes of the property.

The string starts with a T followed by the @encode type and a comma, and finishes with a V followed by the name of the backing instance variable. Between these, the attributes are specified by the following descriptors, separated by commas:

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).(非原子性的,释义为不是线程安全的。atomic是iOS使用的一种线程保护技术,大意是一个进程读写字段时,防止数据错误,另一个进程不允许访问。而这种机制是耗费系统资源的,所以如果没有使用多线程间的通讯编程,那么nonatomic是一个非常好的选择。)
G<name>
The property defines a custom getter selector name. The name follows the G (for example, GcustomGetter,).(用户自定义的get方法名称)
S<name>
The property defines a custom setter selector name. The name follows the S (for example, ScustomSetter:,).(用户自定义的set方法名称)
D
The property is dynamic (@dynamic).(@dynamic告诉编译器,属性的setter与getter方法由用户自己实现,不自动生成。)
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 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


(附上链接https://developer.apple.com/library/prerelease/content/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtPropertyIntrospection.html#//apple_ref/doc/uid/TP40008048-CH101-SW1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios 苹果