Category is the category, also known as categories,
In Objective-C may be added to an existing class attributes Category, but can not add instance variables
Category can only add method, you can not add instance variables
I added a little content category appears to be two of the properties declared here is necessary to achieve the setter and getter methods also do not account for memory and if it does not achieve these two method calls runtime can not find getter and setter methods of course it will crash. Jiedi return to their roots to add that this is also a method. Therefore, the method can only add category
@interface UIView(Basic)
@property (nonatomic) CGFloat current_x;
@property (nonatomic) CGFloat current_y;
.m file
- (CGFloat)current_x
{
return self.frame.origin.x;
}
- (void)setCurrent_x:(CGFloat)current_x {
CGRect frame = self.frame;
frame.origin.x = current_x;
self.frame = frame;
}
But sometimes we need to give some added class attribute, then we can associate Associated Objects to achieve.
tatic const char *badgeViewKey = "badgeViewKey";
@implementation AppDelegate (im)
- (void)setBadgeView:(UIImageView *)badgeView
{
objc_setAssociatedObject(self, badgeViewKey, badgeView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIImageView *)badgeView
{
return objc_getAssociatedObject(self, badgeViewKey);
}
The following excerpt from http://www.cnblogs.com/Ohero/p/4739089.html
From the above two methods to achieve the associated object, we used two different key values
- static void * = & kAssociatedObjectKey kAssociatedObjectKey;
-
With Selector , using the getter method name as key value.
- In fact, there is a static char kAssociatedObjectKey; objc_getAssociatedObject ( Self , & kAssociatedObjectKey);
Comparison of these three ways, many people prefer the second
Related Strategy |
Equivalent properties |
Explanation |
OBJC_ASSOCIATION_ASSIGN |
@property (assign) or @property (unsafe_unretained) |
Weak references associated objects |
OBJC_ASSOCIATION_RETAIN_NONATOMIC |
@property (strong, nonatomic) |
Strong reference associated object, and a non-atomic operation |
OBJC_ASSOCIATION_COPY_NONATOMIC |
@property (copy, nonatomic) |
Copy the associated object, and a non-atomic operation |
OBJC_ASSOCIATION_RETAIN |
@property (strong, atomic) |
Strong reference associated object, and an atomic operation |
OBJC_ASSOCIATION_COPY |
@property (copy, atomic) |
Copy the associated object, and an atomic operation |
- Associated with an object stored in the object itself is associated with no direct relationship, which is stored in a separate hash table;
- Five kinds of associated policies associated with the object qualifier attributes are very similar, in most cases, we will use OBJC_ASSOCIATION_RETAIN_NONATOMIC the associated policies, which can ensure that we hold the associated object;
- The timing of release and remove the timing of associated objects do not always coincide, such as associated with experimental strategy OBJC_ASSOCIATION_ASSIGN when objects associated with it, has long been released, but has not been removed, and then use the associated object to cause Crash.
Reproduced in: https: //www.cnblogs.com/MaybeQueen/p/7762542.html