Association references: Allows developers to attach key-value data to any object, and a common use is to add attributes to the classification.
Upcoming Events
1. Simple Associative references
2. Add the Mbprogresshud Hub property for Uiviewcontroller
3. Add a View property for Uinavigationbar to complete the appearance of dynamically changing Uinavigationbar
The official API is like this, the following blog is also around these to expand
//Association policy enumeration valuetypedef objc_enum (uintptr_t, objc_associationpolicy) {objc_association_assign =0, objc_association_retain_nonatomic =1, objc_association_copy_nonatomic =3, Objc_association_retain =01401, objc_association_copy =01403};/**object source Object key keyword unique static variable keyvalue associated object value (userage) key policy OBJC _association_copy*/Objc_exportvoidObjc_setassociatedobject (ID object,Const void*key, id value, Objc_associationpolicy policy) __osx_available_starting (__mac_10_6, __iphone_3_1);//Get associated objects through Objc_getassociatedobjectObjc_export ID objc_getassociatedobject (ID object,Const void*key) __osx_available_starting (__mac_10_6, __iphone_3_1);//Delete AssociationObjc_exportvoidObjc_removeassociatedobjects (ID object) __osx_available_starting (__mac_10_6, __iphone_3_1);
Scenario 1: You want to use classification to User
add a property called userAge
, the User
class is used in many places, and the user's age is not often used, in order to avoid unnecessary overhead, classification is a good choice.
First I create a User
class
@interface User : NSObject@property (nonatomicNSString *userName;@end
@implementation User@end
Next I'm going to extend the way to User
add a userAge
property.
@interface User (Extensions)@property (nonatomicNSString *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
If this is the only way to use the association reference, I actually feel very far-fetched, expressed hate can not understand ...
The next step is to give a "good" practice in the project. It's embarrassing.
Scenario 2: Extend a hub property for Uiviewcontroller, and then take Mbprogresshud as an example
#import <UIKit/UIKit.h>@interface UIViewController (HUD)- (void)showHudInView:(UIView *)view hint:(NSString *)hint;@end
#import "Uiviewcontroller+hud.h" #import "MBProgressHUD.h" #import <objc/runtime.h> Static Const void* Httpreqhudkey = &httpReqHUDKey; @implementation uiviewcontroller (HUD)-(Mbprogresshud *) hud{returnObjc_getassociatedobject ( Self, Httpreqhudkey);} - (void) Sethud: (Mbprogresshud *) hud{Objc_setassociatedobject ( Self, Httpreqhudkey, HUD, objc_association_retain_nonatomic);} - (void) Showhudinview: (UIView*) View hint: (NSString*) hint{Mbprogresshud *hud = [[Mbprogresshud alloc] initwithview:view]; Hud. LabelText= hint; [View Addsubview:hud]; [HUD Show:YES]; [ SelfSethud:hud];}@end
May see some of the students here have understood a little, plainly, is to extend a property to the original class and implement what we want to do with the property.
Scenario 3 expands an attribute overlay (UIView) for the system Uinavigationbar to achieve an interaction that is popular in many apps, and the explicit function of the navigation bar when sliding the interface-similar to the effect of the simple-book iOS app. The code comes from a well-known three-party library (Ltnavigationbar). Anyway 3000+ 's star. If I remember correctly, only dozens of lines of code, the idea is very good, with the associated attributes, can be found on GitHub. The following is a demo I wrote, you can look at the picture to feel
If you are interested in this demo, you can download it to see the address.
Https://github.com/summerxx27/MyBlogCode facilitates learning and communication.
Weibo-dot I'm a grown man in summer | | QQ Group: 498143780
IOS Rumtime Associated References