iOS开发之Runtime关联属性
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
首先,推薦給大家一個(gè)非常好用的一個(gè)網(wǎng)站:
非盈利無(wú)廣告開(kāi)發(fā)者專(zhuān)用網(wǎng)址導(dǎo)航:http://www.dev666.com/
API介紹
我們先看看Runtime提供的關(guān)聯(lián)API,只有這三個(gè)API,使用也是非常簡(jiǎn)單的:
/*** 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)
設(shè)置關(guān)聯(lián)
實(shí)際上,我們幾乎不會(huì)使用到objc_removeAssociatedObjects函數(shù),這個(gè)函數(shù)的功能是移除指定的對(duì)象上所有的關(guān)聯(lián)。既然我們要添加關(guān)聯(lián)屬性,幾乎不會(huì)存在需要手動(dòng)取消關(guān)聯(lián)的場(chǎng)合。
對(duì)于設(shè)置關(guān)聯(lián),我們需要使用下面的API關(guān)聯(lián)起來(lái):
void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)object:與誰(shuí)關(guān)聯(lián),通常是傳self參數(shù)說(shuō)明:
獲取關(guān)聯(lián)值
如果我們要獲取所關(guān)聯(lián)的值,需要通過(guò)key來(lái)獲取,調(diào)用如下函數(shù):
id objc_getAssociatedObject(id object, const void *key)object:與誰(shuí)關(guān)聯(lián),通常是傳self,在設(shè)置關(guān)聯(lián)時(shí)所指定的與哪個(gè)對(duì)象關(guān)聯(lián)的那個(gè)對(duì)象參數(shù)說(shuō)明:
關(guān)聯(lián)策略
我們先看看設(shè)置關(guān)聯(lián)時(shí)所指定的policy,它是一個(gè)枚舉類(lèi)型,看官方說(shuō)明:
/*** 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:表示弱引用關(guān)聯(lián),通常是基本數(shù)據(jù)類(lèi)型,如int、float,非線程安全我們說(shuō)明一下各個(gè)值的作用:
擴(kuò)展屬性
我們來(lái)寫(xiě)一個(gè)例子,擴(kuò)展UIControl添加Block版本的TouchUpInside事件。
擴(kuò)展頭文件聲明:
#import <UIKit/UIKit.h>typedef void (^HYBTouchUpBlock)(id sender);@interface UIControl (HYBBlock)@property (nonatomic, copy) HYBTouchUpBlock hyb_touchUpBlock;@end擴(kuò)展實(shí)現(xiàn)文件:
?
#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?
非盈利無(wú)廣告開(kāi)發(fā)者專(zhuān)用網(wǎng)址導(dǎo)航:http://www.dev666.com/
?
?
轉(zhuǎn)載于:https://my.oschina.net/u/2448717/blog/750607
總結(jié)
以上是生活随笔為你收集整理的iOS开发之Runtime关联属性的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: postgresql 查看page, i
- 下一篇: c语言知识点(1)