下文會用到const,static關(guān)鍵詞,下一篇文章會針對const,static,extern等關(guān)鍵字的用法詳細介紹,9月18號之前會找空閑編寫上傳,敬請期待,同時歡迎大神指點不足之處-----我的愿望是世界充滿愛
關(guān)于關(guān)聯(lián)引用,簡單的應用場景,舉個栗子:
(1)給一個類動態(tài)添加一個屬性;
(2)給一個類添加一個方法;
官方的API是這樣子的:
//掛連策略,這是類型枚舉,按照字段可以判斷用處
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. */
};
關(guān)聯(lián)對象的三種用法
/*
object 源對象
key 關(guān)鍵字
value 關(guān)聯(lián)的對象
關(guān)鍵策略 上述的枚舉
*/
//設置關(guān)聯(lián)對象
OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
__OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);
//獲取關(guān)聯(lián)對象
OBJC_EXPORT id objc_getAssociatedObject(id object, const void *key)
__OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);
//刪除關(guān)聯(lián)對象
OBJC_EXPORT void objc_removeAssociatedObjects(id object)
__OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);
情景一:動態(tài)添加屬性
有一個User的類,類里面有關(guān)于User的屬性,但是后來你要新添加屬性,User這個類會經(jīng)常用到,但是你新添加的屬性卻不怎么用到,為了節(jié)省內(nèi)存,造成不必要的系統(tǒng)開銷,所以做一個動態(tài)添加屬性
1.創(chuàng)建一個User類,基于NSObject
#import <Foundation/Foundation.h>
@interface User : NSObject
@property(nonatomic,copy)NSString *userName;
@end
2.需要添加一個userAge的屬性
創(chuàng)建一個基于User的類別
#import "User.h"
@interface User (Extensions)
@property(nonatomic,copy)NSString *userAge;
@end
#import "User+Extensions.h"
#import <objc/runtime.h>
@implementation User (Extensions)
static char userAgeKey;
-(NSString *)userAge{
return objc_getAssociatedObject(self,&userAgeKey);
}
-(void)setUserAge:(NSString *)userAge{
objc_setAssociatedObject(self, &userAgeKey, userAge, OBJC_ASSOCIATION_COPY);
}
@end
這就是最簡單的一個使用場景
場景二:給UIViewController添加一個方法
給UIViewController添加一個HUD的方法
1.基于UIViewController創(chuàng)建一個類別,然后聲明一個方法
#import <UIKit/UIKit.h>
#import "MBProgressHUD.h"
@interface UIViewController (HUD)
-(void)showHudInView:(UIView *)view
hint:(NSString *)hint;
@end
2.實現(xiàn)方法
#import "UIViewController+HUD.h"
#import <objc/runtime.h>
static const void * httpReaHud = &httpReaHud;
@implementation UIViewController (HUD)
-(MBProgressHUD *)hud{
return objc_getAssociatedObject(self, httpReaHud);
}
-(void)setHud:(MBProgressHUD *)hud{
objc_setAssociatedObject(self, httpReaHud, hud, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(void)showHudInView:(UIView *)view hint:(NSString *)hint{
MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:view];
HUD.label.text = hint;
[view addSubview:HUD];
[HUD showAnimated:YES];
[self setHud:HUD];
}
@end
這樣從項目的整潔度,簡潔度還有節(jié)省內(nèi)存都有好處
這個是demo,有需要的可以簡單的看一下https://github.com/summer7659/runtime-