readonly属性对Objective-C@property指令的影响
这是我的情况,我宣布一个objective-c类如下:readonly属性对Objective-C@property指令的影响,objective-c,xcode,properties,Objective C,Xcode,Properties,这是我的情况,我宣布一个objective-c类如下: @interface FileItem : NSObject @property (nonatomic, strong, readonly) NSString *path; @property (nonatomic, strong, readonly) NSArray* childItems; - (id)initWithPath:(NSString*)path; @end @implementation LCDFileItem - (
@interface FileItem : NSObject
@property (nonatomic, strong, readonly) NSString *path;
@property (nonatomic, strong, readonly) NSArray* childItems;
- (id)initWithPath:(NSString*)path;
@end
@implementation LCDFileItem
- (id)initWithPath:(NSString*)path {
self = [super init];
if (self) {
_path = path;
}
return self;
}
- (NSArray*)childItems {
if (!_childItems) { // error, _childItems undeclared!
NSError *error;
NSMutableArray *items = [[NSMutableArray alloc] init];
_childItems = items; // error, _childItems undeclared!
}
return _childItems; // error, _childItems undeclared!
}
我将“path”和“childItems”标记为只读属性,编译器抱怨“\u childItems”标识符未声明,似乎我不能使用“-(NSArray*)childItem”作为“childItem”属性的getter函数(我更改了函数名,一切正常),为什么?
我知道“只读”属性会使xcode忽略属性的setter函数,但对getter函数有什么影响?xcode会抱怨,因为代码的问题是,如果您实现了所有所需的访问器方法,则属性不会自动合成,对于只读属性来说,它只是实现getter 在@implementation之后添加以下内容:
@synthesize childItems = _childItems;
错误应该会消失…Xcode抱怨,因为代码的问题是,如果您实现了所有所需的访问器方法,则属性不会自动合成,而对于只读属性,这些方法只是实现了getter 在@implementation之后添加以下内容:
@synthesize childItems = _childItems;
错误应该会消失…Xcode抱怨,因为代码的问题是,如果您实现了所有所需的访问器方法,则属性不会自动合成,而对于只读属性,这些方法只是实现了getter 在@implementation之后添加以下内容:
@synthesize childItems = _childItems;
错误应该会消失…Xcode抱怨,因为代码的问题是,如果您实现了所有所需的访问器方法,则属性不会自动合成,而对于只读属性,这些方法只是实现了getter 在@implementation之后添加以下内容:
@synthesize childItems = _childItems;
错误应该会消失…来自苹果的文档:“如果您同时实现了readwrite属性的getter和setter,或readonly属性的getter,,编译器将假定您正在控制属性实现,并且不会自动合成实例变量。”
一种常见的方法是将“readonly”属性放在标题中,将重复的属性定义放在类扩展中,而不使用“readonly”属性。来自Apple文档:“如果您同时为readwrite属性实现了getter和setter,或为readonly属性实现了getter,,编译器将假定您正在控制属性实现,并且不会自动合成实例变量。”
一种常见的方法是将“readonly”属性放在标题中,将重复的属性定义放在类扩展中,而不使用“readonly”属性。来自Apple文档:“如果您同时为readwrite属性实现getter和setter,或为readonly属性实现getter,,编译器将假定您正在控制属性实现,并且不会自动合成实例变量。”
一种常见的方法是将“readonly”属性放在标题中,将重复的属性定义放在类扩展中,而不使用“readonly”属性。来自Apple文档:“如果您同时为readwrite属性实现getter和setter,或为readonly属性实现getter,,编译器将假定您正在控制属性实现,并且不会自动合成实例变量。”
一种常见的方法是将“readonly”属性放在标题中,将重复的属性定义放在类扩展中,而不使用“readonly”属性