【发布时间】:2014-08-25 13:44:19
【问题描述】:
摘要:
这个问题是关于属性的继承以及从相互继承属性的类的内部和外部的不同读/写访问。
详情:
我有一个类A 和另一个类B,它继承自A。在A 中声明了属性someProperty。我希望该属性在这些类之外是只读的,而从内部是读/写的。
只有一个类,这非常简单:您将.h 中的属性声明为只读,然后在类别内的.m 中再次将其声明为可读写。完成。
但是对于这两个类,一个派生自另一个,我在B 中收到以下编译器警告:
自动属性合成不会合成属性“someProperty” 因为它是“读写”,但它将通过“只读”合成 另一个属性
代码如下:
啊哈:
#import <Foundation/Foundation.h>
@interface A : NSObject
// This property shall be readonly from outside, but read/write from subclasses
@property (readonly) SInt32 someProperty;
@end
上午:
#import "A.h"
@implementation A
@end
B.h:
#import <Foundation/Foundation.h>
#import "A.h"
@interface B : A
@end
B.m:
#import "B.h"
@interface B ()
// compiler warning in the following property declaration:
// /Users/.../B.m:12:41: Auto property synthesis will not synthesize property
// 'someProperty' because it is 'readwrite' but it will be synthesized
// 'readonly' via another property
@property (readwrite) SInt32 someProperty;
@end
@implementation B
@end
为什么会出现这个警告,我应该如何构建我的代码以避免它?
【问题讨论】:
标签: objective-c inheritance properties