setting new properties in category interface/implementation

后端 未结 7 1442
醉梦人生
醉梦人生 2020-12-03 03:42

Ok, so I have this, but it wont work:

@interface UILabel (touches)

@property (nonatomic) BOOL isMethodStep;

@end


@implementation UILabel (touches)

-(BOO         


        
相关标签:
7条回答
  • 2020-12-03 04:23

    It is not possible to add members and properties to an existing class via a category — only methods.

    https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/Category.html

    One possible workaround is to write "setter/getter-like" methods, that uses a singleton to save the variables, that would had been the member.

    -(void)setMember:(MyObject *)someObject
    {
        NSMutableDictionary *dict = [MySingleton sharedRegistry];
        [dict setObject:someObject forKey:self];
    }
    
    -(MyObject *)member
    {
        NSMutableDictionary *dict = [MySingleton sharedRegistry];
        return [dict objectforKey:self];
    }
    

    or — of course — write a custom class, that inherits from UILabel


    Note that nowadays an associated object can be injected during runtime. The Objective C Programming Language: Associative References

    0 讨论(0)
提交回复
热议问题