Objective-C中的类扩展 [英] class extension in objective-c
问题描述
我正在通过读书来学习对象c.当我阅读有关类扩展的章节时,这本书给出了以下示例代码:
// A class extension
@interface BNREmployee ()
@property (nonatomic) unsigned int officeAlarmCode;
@end
@implementation BNREmployee
...
@end
这本书说,不是BNREmployee
实例的对象不能再看到此属性 officeAlarmCode
.举个例子:
BNREmployee *mikey = [[BNREmployee alloc] init];
unsigned int mikeysCode = mikey.officeAlarmCode;
这种尝试将导致编译器错误,显示为没有可见的@interface声明实例方法officeAlarmCode".
但是我很困惑.我的意思是我感觉到书中的字眼&它的示例代码是矛盾的.书中说,不是BNREmployee
实例的对象不能再看到属性officeAlarmCode
.但是在上面的示例代码中,mikey
不是BNREmployee
的实例吗?为什么它看不到officeAlarmCode
事件,它是BNREmployee
的实例?
===更新=====
我正在阅读的书是这本书.第22章,第162页.
我只想验证这本书以误导性的方式解释了&我在这里寻求明确的解释.因为书说"BNREmployee
的 NOT 实例的对象不再可以看到属性officeAlarmCode",对于像我这样的书阅读器,我觉得它暗示了BNREmployee
CAN SEE属性的实例的对象officeAlarmCode
.这就是我感到困惑的原因,因为mikey
是BNREmployee
的实例,但是它无法访问officeAlarmCode.
按照 您可以创建 I am learning object-c by reading a book. When I read the chapter about class extension, the book gives the following example code: The book says objects that are not instances of This attempt would result in a compiler error that reads "No visible @interface declares the instance method officeAlarmCode". But I get confused. I mean I feel the book's words & its example code are contradictory. Book says objects that are not instance of === UPDATE ===== The book I am reading is this one. Chapter 22, page 162. I just want to verify that the book explained in a misleading way & I am looking for a clear explanation here. Because book says "objects that are NOT instance of As per the Apple Docs
1. a class extension can add its own properties and instance variables to a class
2. Class extensions are often used to extend the public interface with additional private methods or properties for use within the implementation of the class itself. so if you declare the property in class extension it will be visible only to the implementation file. like in If you want to use officeAlarmCode in other classes, let's say OtherEmployee class then you need to create officeAlarmCode property in BNREmployee.h file with readOnly or readWrite access. Then you can use it like in you can create instance of
这篇关于Objective-C中的类扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!BNREmployee
的实例,并可以为otherAlarmCode
属性分配officeAlarmCode
值,如下所示BNREmployee *bnrEmployee = [BNREmployee alloc] init];
_otherAlarmCode = bnrEmployee.officeAlarmCode;
// A class extension
@interface BNREmployee ()
@property (nonatomic) unsigned int officeAlarmCode;
@end
@implementation BNREmployee
...
@end
BNREmployee
can no longer see this property officeAlarmCode
. It makes an example:BNREmployee *mikey = [[BNREmployee alloc] init];
unsigned int mikeysCode = mikey.officeAlarmCode;
BNREmployee
can not longer see property officeAlarmCode
. But in the example code above, isn't mikey
an instance of BNREmployee
? Why it cannot see officeAlarmCode
event it is an instance of BNREmployee
?BNREmployee
can no longer see property officeAlarmCode", for the book reader like me, I feel it hints objects that are instance of BNREmployee
CAN SEE property officeAlarmCode
. That's why I am confusing, because mikey
is an instance of BNREmployee
but it can't access officeAlarmCode.BNREmployee.m
@interface BNREmployee ()
@property (nonatomic) unsigned int officeAlarmCode;
@end
@implementation BNREmployee
- (void) someMethod {
//officeAlarmCode will be available inside implementation block to use
_officeAlarmCode = 10;
}
@end
BNREmployee.h
@property (nonatomic, readOnly) unsigned int officeAlarmCode; //readOnly you can just read not write
OtherEmployee.m
import "BNREmployee.h"
@interface OtherEmployee ()
@property (nonatomic) unsigned int otherAlarmCode;
@end
@implementation OtherEmployee
BNREmployee
and can assign officeAlarmCode
value to otherAlarmCode
property like belowBNREmployee *bnrEmployee = [BNREmployee alloc] init];
_otherAlarmCode = bnrEmployee.officeAlarmCode;