Objective-C中的类扩展 [英] class extension in objective-c

查看:111
本文介绍了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.这就是我感到困惑的原因,因为mikeyBNREmployee的实例,但是它无法访问officeAlarmCode.

解决方案

按照

您可以创建BNREmployee的实例,并可以为otherAlarmCode属性分配officeAlarmCode值,如下所示

BNREmployee *bnrEmployee = [BNREmployee alloc] init];
_otherAlarmCode = 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:

// A class extension
@interface BNREmployee ()

@property (nonatomic) unsigned int officeAlarmCode;

@end

@implementation BNREmployee
...
@end

The book says objects that are not instances of BNREmployee can no longer see this property officeAlarmCode. It makes an example:

BNREmployee *mikey = [[BNREmployee alloc] init];
unsigned int mikeysCode = mikey.officeAlarmCode;

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 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 ?

=== 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 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.

解决方案

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 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

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

BNREmployee.h

@property (nonatomic, readOnly) unsigned int officeAlarmCode; //readOnly you can just read not write 

in OtherEmployee.m

import "BNREmployee.h"
@interface OtherEmployee ()

@property (nonatomic) unsigned int otherAlarmCode;

@end

@implementation OtherEmployee

you can create instance of BNREmployee and can assign officeAlarmCode value to otherAlarmCode property like below

BNREmployee *bnrEmployee = [BNREmployee alloc] init];
_otherAlarmCode = bnrEmployee.officeAlarmCode;

这篇关于Objective-C中的类扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆