日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

iOS开发之Runtime关联属性

發(fā)布時(shí)間:2023/12/4 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS开发之Runtime关联属性 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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ō)明:

  • key:唯一鍵,在獲取值時(shí)通過(guò)該鍵獲取,通常是使用static const void *來(lái)聲明
  • value:關(guān)聯(lián)所設(shè)置的值
  • policy:內(nèi)存管理策略,比如使用copy
  • 獲取關(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ō)明:

  • key:唯一鍵,在設(shè)置關(guān)聯(lián)時(shí)所指定的鍵
  • 關(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è)值的作用:

  • OBJC_ASSOCIATION_RETAIN_NONATOMIC:表示強(qiáng)(strong)引用關(guān)聯(lián)對(duì)象,非線程安全
  • OBJC_ASSOCIATION_COPY_NONATOMIC:表示關(guān)聯(lián)對(duì)象copy,非線程安全
  • OBJC_ASSOCIATION_RETAIN:表示強(qiáng)(strong)引用關(guān)聯(lián)對(duì)象,是線程安全的
  • OBJC_ASSOCIATION_COPY:表示關(guān)聯(lián)對(duì)象copy,是線程安全的
  • 擴(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)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。