[英]class extension in objective-c
我正在通过阅读书本来学习object-c。 当我阅读关于类扩展的章节时,本书给出了以下示例代码:
// A class extension
@interface BNREmployee ()
@property (nonatomic) unsigned int officeAlarmCode;
@end
@implementation BNREmployee
...
@end
该书说,不是BNREmployee
实例的BNREmployee
不能再看到这个属性 officeAlarmCode
。 它举了一个例子:
BNREmployee *mikey = [[BNREmployee alloc] init];
unsigned int mikeysCode = mikey.officeAlarmCode;
此尝试将导致编译器错误,其中显示“No visible @interface声明实例方法officeAlarmCode”。
但是我很困惑。 我的意思是我觉得这本书的文字及其示例代码是矛盾的。 本书说,不是BNREmployee
实例的BNREmployee
不能再看到属性officeAlarmCode
。 但是,在上面的示例代码,是不是mikey
的实例BNREmployee
? 为什么它看不到officeAlarmCode
事件它是BNREmployee
一个实例?
===更新=====
我只想验证这本书是以误导的方式解释的,我在这里寻找明确的解释。 因为书上说“那不是的实例对象BNREmployee
,像我这样的电子书阅读器不再能看到物业officeAlarmCode”,我觉得这暗示,是实例对象BNREmployee
可以看到物业officeAlarmCode
。 这就是为什么我很困惑,因为mikey
是一个实例BNREmployee
但它不能访问officeAlarmCode。
Mikes是BNREmployee的一个实例。 但是该示例告诉您,属性officeAlarmCode
没有公开,只能由BNREmployee对象在内部使用。
说“物体再也看不见”有点误导。 即使在比喻意义上,物体也看不到任何东西。 让我们说: 代码放置在类的实现之外无法看到属性。
根据Apple Docs1 。类扩展可以将自己的属性和实例变量添加到类2。类扩展通常用于扩展具有其他私有方法或属性的公共接口,以供在类本身的实现中使用。
因此,如果您在类扩展中声明该属性,则该属性仅对实现文件可见。 喜欢
在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
如果要在其他类中使用officeAlarmCode,例如,OtherEmployee类,则需要在BNREmployee.h文件中创建具有readOnly或readWrite访问权限的officeAlarmCode属性。 那你可以像使用它一样
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
和可分配officeAlarmCode
值otherAlarmCode
财产像下面
BNREmployee *bnrEmployee = [BNREmployee alloc] init];
_otherAlarmCode = bnrEmployee.officeAlarmCode;
BNREmployee *mikey = [[BNREmployee alloc] init];
unsigned int mikeysCode = mikey.officeAlarmCode;
考虑上下文,只要扩展接口在给定上下文中“可见”,任何其他类都可以访问在扩展名或任何类别中声明的属性。
例如,以下实现文件包含两个接口的实现: BaseObject
和BaseObjectController
。 在其他类( BaseObjectController
)的实现中,您可以通过getter和setter安全地使用“hidden”属性,因为声明接口是“可见的”。 如果将BaseObjectController
的实现BaseObjectController
看不到扩展声明的另一个文件-该代码将无法编译。
#import "BaseObject.h"
#import "BaseObjectController.h"
// BaseObject
@interface BaseObject()
@property (strong) NSString * idString;
@end
@implementation BaseObject
@end
// BaseObjectController
@implementation BaseObjectController
- (void) initBaseObject {
BaseObject * bo = [BaseObject new];
bo.idString = @"01234";
}
@end
我从中得到的是,物业officeAlarmCode
只是中可见BNREmployee.m
文件,它不会从访问main.m
。
要通过价值mikeysCode
你必须创建一个返回的方法officeAlarmCode
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.