ѧϰyykit һЩСС×ܽá

2018-07-20    À´Ô´£º±à³ÌѧϰÍø

ÈÝÆ÷ÔÆÇ¿ÊÆÉÏÏߣ¡¿ìËٴ¼¯Èº£¬ÉÏÍòLinux¾µÏñËæÒâʹÓÃ


×òÌì¿´ÁËyykit ×¢Òâµ½ÓÐÒ»¶Î¸øcategory ¶¯Ì¬Ìí¼ÓÊôÐԵĺ꣬ҲûÉîÈëÑо¿£¬½ñÌìÀ´×Ðϸ¿´¿´

#ifndef YYSYNTH_DYNAMIC_PROPERTY_OBJECT #define YYSYNTH_DYNAMIC_PROPERTY_OBJECT(_getter_, _setter_, _association_, _type_) \ - (void)_setter_ : (_type_)object { \
    [self willChangeValueForKey:@#_getter_]; \ objc_setAssociatedObject(self, _cmd, object, OBJC_ASSOCIATION_ ## _association_); \ [self didChangeValueForKey:@#_getter_]; \ } \
- (_type_)_getter_ { \ return objc_getAssociatedObject(self, @selector(_setter_:)); \
} #endif

Ëûµ÷Óõķ½Ê½ÊÇÕâÑùµÄ

//µ÷Óõķ½·¨  @interface NSObject (MyAdd)  @property (nonatomic, retain) UIColor *myColor;  @end #import <objc/runtime.h>  @implementation NSObject (MyAdd) YYSYNTH_DYNAMIC_PROPERTY_OBJECT(myColor, setMyColor, RETAIN, UIColor *)  @end

¿´ÁËÉÏÃæÒ»¶Îºê¶¨Òå ºÍ µ÷Ó㬴ó¼ÒÓ¦¸Ã»¹ÊDz»Ì«Á˽âΪʲôÄǶκ궨Òå¾Í¿ÉÒÔʵÏÖ¶¯Ì¬¸øcategory Ìí¼ÓÊôÐÔ¡£
ÏÈÆÕ¼°Ò»µãºê¶¨Òå µÄ¹Ø¼ü×Ö Ò»¸ö# ±íʾҪÌæ»»±äÁ¿ £¬Á½¸ö ##Ҫƴ½Ó×Ö·û´® £¬\±íʾ¶àÐÐ cmd ÊÇiOS ÊÇÒ»ÖÖÌض¨ºê £¬±íʾ±¾µØ·½·¨£¬ ÔÚÕâÀï±íʾ @selector(_setter:)
ºÃµÄ£¬½ÓÏÂÀ´£¬Ð½¨Ò»¸öproject À´Ä£·ÂÕâ¸ö°¸Àý£¬Ò²¼´ÊÇÈçºÎ¸øcategory ¶¯Ì¬Ìí¼ÓÊôÐÔ£¬ÆäʵÕâÀï¹Ø¼üÊÇÊÇÓõ½ÁË runtime µÄ¶¯Ì¬¹ØÁª£¬Ê¹ÓÃAssociate ½¨Á¢¹ØÁª¶¯Ì¬Ìí¼ÓÊôÐÔ
Èç¹û½«ºê¶¨Òåת»»³É´úÂ뽫»áÕâôдÄØ

-(void)setMyColor:(UIColor *)myColor
{
    [self willChangeValueForKey:@"myColor"];//kvo »úÖƼ´½«Ð޸ıäÁ¿·¢ËÍ֪ͨ
    //1 Ô´¶ÔÏó
    //2 ¹Ø¼ü×Ö Î¨Ò»¾²Ì¬±äÁ¿key associatedkey
    //3 ¹ØÁªµÄ¶ÔÏó sender
    //4 ¹Ø¼ü²ßÂÔ  OBJC_ASSOCIATION_RETAIN_NONATOMIC
    objc_setAssociatedObject(self, @selector(setMyColor:), myColor, OBJC_ASSOCIATION_RETAIN);
    //
    [self didChangeValueForKey:@"myColor"];//kvo »úÖÆÒÑÐ޸ıäÁ¿·¢ËÍ֪ͨ
}
-(UIColor *)myColor
{ return objc_getAssociatedObject(self, @selector(setMyColor:));
}

¹À¼Æµ½×îºóÓ¦¸Ã»¹»áÓÐÕâôһ¸öÎÊÌ⣬ΪʲôעÊÍ2 ¹Ø¼ü×Öд³ÉÁËÕâÖÖ@selector(setMyColor:) ÕâÖÖÐÎʽÁË£¬Æäʵ¹Ø¼ü×ÖÊÇÒ»¸övoidÀàÐ͵ÄÖ¸Õ롣ÿһ¸ö¹ØÁªµÄ¹Ø¼ü×Ö±ØÐëÊÇΨһµÄ¡£Í¨³£¶¼ÊÇ»á²ÉÓþ²Ì¬±äÁ¿À´×÷Ϊ¹Ø¼ü×Ö¡£ËùÒÔÕâÀïÓÃ@selector(setMyColor:)»ñÈ¡void ÀàÐÍÖ¸ÕëÒ²ÊÇ¿ÉÒÔµÄ


ÎÄ£¯ºÙÇçÌ죨¼òÊé×÷Õߣ©
Ô­ÎÄÁ´½Ó£ºhttp://www.jianshu.com/p/911d737c6703

±êÇ©£º idc ´úÂë

°æȨÉêÃ÷£º±¾Õ¾ÎÄÕ²¿·Ö×ÔÍøÂ磬ÈçÓÐÇÖȨ£¬ÇëÁªÏµ£ºwest999com@outlook.com
Ìرð×¢Ò⣺±¾Õ¾ËùÓÐתÔØÎÄÕÂÑÔÂÛ²»´ú±í±¾Õ¾¹Ûµã£¡
±¾Õ¾ËùÌṩµÄͼƬµÈËزģ¬°æȨ¹éÔ­×÷ÕßËùÓУ¬ÈçÐèʹÓã¬ÇëÓëÔ­×÷ÕßÁªÏµ¡£

ÉÏһƪ:Ö»ÖªµÀAPI£¬½ñÌì³õʶSPI

ÏÂһƪ:TIOBE 2016Äê3Ô±à³ÌÓïÑÔÅÅÐаñ£º±à³ÌÓïÑÔ³ÊÄÚ²¿·¢Õ¹Ç÷ÊÆ