iOS开发之Runtime关联属性
生活随笔
收集整理的這篇文章主要介紹了
iOS开发之Runtime关联属性
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
首先,推薦給大家一個非常好用的一個網站:
非盈利無廣告開發者專用網址導航:http://www.dev666.com/
API介紹
我們先看看Runtime提供的關聯API,只有這三個API,使用也是非常簡單的:
/*** Sets an associated value for a given object using a given key and association policy.** @param object The source object for the association.* @param key The key for the association.* @param value The value to associate with the key key for object. Pass nil to clear an existing association.* @param policy The policy for the association. For possible values, see “Associative Object Behaviors.”** @see objc_setAssociatedObject* @see objc_removeAssociatedObjects*/void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)/*** Returns the value associated with a given object for a given key.** @param object The source object for the association.* @param key The key for the association.** @return The value associated with the key \e key for \e object.** @see objc_setAssociatedObject*/id objc_getAssociatedObject(id object, const void *key)/*** Removes all associations for a given object.** @param object An object that maintains associated objects.** @note The main purpose of this function is to make it easy to return an object*??to a "pristine state”. You should not use this function for general removal of*??associations from objects, since it also removes associations that other clients*??may have added to the object. Typically you should use \c objc_setAssociatedObject*??with a nil value to clear an association.** @see objc_setAssociatedObject* @see objc_getAssociatedObject*/void objc_removeAssociatedObjects(id object)
設置關聯
實際上,我們幾乎不會使用到objc_removeAssociatedObjects函數,這個函數的功能是移除指定的對象上所有的關聯。既然我們要添加關聯屬性,幾乎不會存在需要手動取消關聯的場合。
對于設置關聯,我們需要使用下面的API關聯起來:
void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)object:與誰關聯,通常是傳self參數說明:
獲取關聯值
如果我們要獲取所關聯的值,需要通過key來獲取,調用如下函數:
id objc_getAssociatedObject(id object, const void *key)object:與誰關聯,通常是傳self,在設置關聯時所指定的與哪個對象關聯的那個對象參數說明:
關聯策略
我們先看看設置關聯時所指定的policy,它是一個枚舉類型,看官方說明:
/*** Policies related to associative references.* These are options to objc_setAssociatedObject()*/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. */};
OBJC_ASSOCIATION_ASSIGN:表示弱引用關聯,通常是基本數據類型,如int、float,非線程安全我們說明一下各個值的作用:
擴展屬性
我們來寫一個例子,擴展UIControl添加Block版本的TouchUpInside事件。
擴展頭文件聲明:
#import <UIKit/UIKit.h>typedef void (^HYBTouchUpBlock)(id sender);@interface UIControl (HYBBlock)@property (nonatomic, copy) HYBTouchUpBlock hyb_touchUpBlock;@end擴展實現文件:
?
#import "UIControl+HYBBlock.h"#import <objc/runtime.h>static const void *sHYBUIControlTouchUpEventBlockKey = "sHYBUIControlTouchUpEventBlockKey";@implementation UIControl (HYBBlock)- (void)setHyb_touchUpBlock:(HYBTouchUpBlock)hyb_touchUpBlock {objc_setAssociatedObject(self,sHYBUIControlTouchUpEventBlockKey,hyb_touchUpBlock,OBJC_ASSOCIATION_COPY);[self removeTarget:selfaction:@selector(hybOnTouchUp:)forControlEvents:UIControlEventTouchUpInside];if (hyb_touchUpBlock) {[self addTarget:selfaction:@selector(hybOnTouchUp:)forControlEvents:UIControlEventTouchUpInside];}}- (HYBTouchUpBlock)hyb_touchUpBlock {return objc_getAssociatedObject(self, sHYBUIControlTouchUpEventBlockKey);}- (void)hybOnTouchUp:(UIButton *)sender {HYBTouchUpBlock touchUp = self.hyb_touchUpBlock;if (touchUp) {touchUp(sender);}}@end?
非盈利無廣告開發者專用網址導航:http://www.dev666.com/
?
?
轉載于:https://my.oschina.net/u/2448717/blog/750607
總結
以上是生活随笔為你收集整理的iOS开发之Runtime关联属性的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: postgresql 查看page, i
- 下一篇: c语言知识点(1)