【问题标题】:Class and class extension (category) in the same header file同一个头文件中的类和类扩展名(类别)
【发布时间】:2015-02-24 06:04:27
【问题描述】:

当我查看 Cocoa Touch API 时,我可以在同一个头文件中找到一些与类别一起声明的类,例如

@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSSecureCoding, NSFastEnumeration>

@property (readonly) NSUInteger count;
// and some other properties

@end

@interface NSArray (NSExtendedArray)

@property (readonly, copy) NSString *description;
// and some other properties

@end

现在我正在尝试对我的班级做同样的事情,如下所示:

@interface ARCTextbook : NSObject

@property (nonatomic) NSInteger ID;
@property (nonatomic) NSString *name;

@end

@interface ARCTextbook (Student)

@property (nonatomic) NSInteger studentID;
@property (nonatomic, getter=isUsed) BOOL used; // Used by a student?

@end

但是,当我尝试访问 studentIDused 属性时,出现无法识别的选择器错误。我错过了什么吗?

干杯。

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    这是关联对象,您可以参考以下文档:

    http://www.davidhamrick.com/2012/02/12/Adding-Properties-to-an-Objective-C-Category.html

    How to store not id type variable use a objc_getAssociatedObject/objc_setAssociatedObject?

    ARCTextbook.h

    #import <Foundation/Foundation.h>
    
    @interface ARCTextbook : NSObject
    @property (nonatomic) NSInteger ID;
    @property (nonatomic) NSString *name;
    @end
    
    @interface ARCTextbook (Student)
    
    @property (nonatomic) NSInteger studentID;
    @property (nonatomic, getter=isUsed) BOOL used; // Used by a student?
    
    @end
    

    ARCTextbook.m

    #import "ARCTextbook.h"
    #import <objc/runtime.h>
    
    @implementation ARCTextbook
    
    @end
    
    static NSString *kStudentID = @"kStudentID";
    static NSString *kUsed = @"kUsed";
    
    @implementation ARCTextbook (Student)
    @dynamic studentID;
    @dynamic used;
    
    - (void)setStudentID:(NSInteger)aStudentID {
        objc_setAssociatedObject(self, (__bridge const void *)(kStudentID), @(aStudentID), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    - (NSInteger)studentID {
        return [objc_getAssociatedObject(self, (__bridge const void *)(kStudentID)) integerValue];
    }
    
    - (void)setUsed:(BOOL)aUsed {
        objc_setAssociatedObject(self, (__bridge const void *)(kUsed), @(aUsed), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    - (BOOL)isUsed {
        return [objc_getAssociatedObject(self, (__bridge const void *)(kUsed)) boolValue];
    }
    
    @end
    

    ViewController.m

    #import "ViewController.h"
    #import "ARCTextbook.h"
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // Do any additional setup after loading the view.
        ARCTextbook *t = [[ARCTextbook alloc] init];
        t.studentID = 2;
        t.used = YES;
    
    }
    
    @end
    

    【讨论】:

    • 你试过运行代码吗?我在运行时遇到了无法识别的选择器错误。
    • 您好,感谢您的回答。我想除了使用关联对象没有办法解决这个问题!
    猜你喜欢
    • 2021-05-02
    • 1970-01-01
    • 2011-03-30
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    • 2011-09-01
    • 2012-01-23
    相关资源
    最近更新 更多