Objective-C is an amazing skill -- used to add macros to attributes in category, and objective-c

Source: Internet
Author: User

Objective-C is an amazing skill -- used to add macros to attributes in category, and objective-c

Technical lust refers to technologies and products that are too clever and useless.
Reprinted please indicate the source http://blog.csdn.net/uxyheaven/article/details/46789065

As we all know, in general, we cannot add attributes to category.
Associated is required if you want to add it.

@interface NSObject (XYFlyweightTransmit)@property (nonatomic, strong) id uxy_flyweightData;@end@implementation NSObject (UXYFlyweightTransmit)- (id)uxy_flyweightData{    return objc_getAssociatedObject(self, NSObject_key_flyweightData);}- (void)setUxy_flyweightData:(id)flyweightData{    objc_setAssociatedObject(self, NSObject_key_flyweightData, flyweightData, OBJC_ASSOCIATION_RETAIN_NONATOMIC);}@end;

If you need to write so much code each time to implement it, it is really complicated.
Generally, we encapsulate the following code:

@interface NSObject (XY_associated)- (id)uxy_getAssociatedObjectForKey:(const char *)key;- (id)uxy_retainAssociatedObject:(id)obj forKey:(const char *)key;- @end@implementation NSObject (XY_associated)- (id)uxy_getAssociatedObjectForKey:(const char *)key{    const char * propName = key;    id currValue = objc_getAssociatedObject( self, propName );    return currValue;}- (id)uxy_retainAssociatedObject:(id)obj forKey:(const char *)key;{    const char * propName = key;    id oldValue = objc_getAssociatedObject( self, propName );    objc_setAssociatedObject( self, propName, obj, OBJC_ASSOCIATION_RETAIN_NONATOMIC );    return oldValue;}

In this way, we only need to add an attribute as follows:

- (UIView *)overlay{    return [self uxy_getAssociatedObjectForKey:"xy.navigationBar.overlay"];}- (void)setOverlay:(UIView *)overlay{    [self uxy_retainAssociatedObject:overlay forKey:"xy.navigationBar.overlay"];}

The above Code seems to be a lot of repetitive. We are using a macro to encapsulate it.
However, when we really write macros, we find that the get method is easy to write, and the set Method is useless, because it is followed by an uppercase letter.
The following solution is generally used:
* The agreed attribute must start with an uppercase letter.
* Starting with an underscore
* When writing a macro, pass in the setName name.

However, the title of this article is not the above method.
Let's take a look at what the code looks like.

@interface UINavigationBar (XY)uxy_property_as_associated_strong(id, test2);@end@implementation UINavigationBar (XY)uxy_property_def_associated_strong(id, test2)@end{    self.test2 = @"a";    id c = self.test2;    NSLog(@"%@", c);}

Implementation ideas:
When declaring an attribute, use setter to modify the set Method of the attribute, and add _ in front to avoid case sensitivity.

The specific implementation code is as follows:

#define uxy_property_as_associated_strong( __type, __name) \        @property (nonatomic, strong, setter=set__##__name:, getter=__##__name) __type __name;#define uxy_property_def_associated_strong( __type, __name) \        - (__type)__##__name   \        { return [self uxy_getAssociatedObjectForKey:#__name]; }   \        - (void)set__##__name:(id)__##__name   \        { [self uxy_retainAssociatedObject:__##__name forKey:#__name]; }

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.