很簡單,看下注釋就明白了。
github代碼
@implementation UIViewController (Extension)
/**
* 此方法并不是修改了類本身的結構,原理是通過添加關聯對象的方法實現類似的效果
*/
static char *name;
- (void)setLy_name:(NSString *)ly_name
{
objc_setAssociatedObject(self, name, ly_name, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSString *)ly_name
{
return objc_getAssociatedObject(self, name);
}
/**
*
* void objc_setAssociatedObject(id object, void *key, id value, objc_AssociationPolicy policy)
*
* Parameters
* object The source object for the association. 給誰添加關聯對象
* key The key for the association. 關聯對象的原理是kvc,所以key一定不能變,要用static修飾。
* value The value to associate with the key key for object. 值
* policy The policy for the association. For possible values. 如下 類似 assign,copy,stong等
*/
//typedef OBJC_ENUM(uintptr_t, objc_AssociationPolicy) {
// OBJC_ASSOCIATION_ASSIGN = 0, /**< Specifies a weak reference to the associated object. */
// OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, /**< Specifies a strong reference to the associated object.
// * The association is not made atomically. */
// OBJC_ASSOCIATION_COPY_NONATOMIC = 3, /**< Specifies that the associated object is copied.
// * The association is not made atomically. */
// OBJC_ASSOCIATION_RETAIN = 01401, /**< Specifies a strong reference to the associated object.
// * The association is made atomically. */
// OBJC_ASSOCIATION_COPY = 01403 /**< Specifies that the associated object is copied.
// * The association is made atomically. */
//};