首页 > 系统 > iOS > 正文

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

2019-11-08 00:21:54
字体:
来源:转载
供稿:网友

苹果官方文档()如下: 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:

RThe property is read-only (readonly).(只读属性)CThe property is a copy of the value last assigned (copy).(复制属性)&The property is a reference to the value last assigned (retain).(持有属性)NThe 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方法名称)DThe property is dynamic (@dynamic).(@dynamic告诉编译器,属性的setter与getter方法由用户自己实现,不自动生成。)WThe property is a weak reference (__weak).(弱引用属性)PThe 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)


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表