我已阅读 属性重新声明章节rel="nofollow">The Objective-C 编程语言 文档,我希望你们中的一些人可以向我澄清以下属性重新声明:
// MyObject.h public header file
@interface MyObject : NSObject {
NSString *language;
}
@property (readonly, copy) NSString *language;
@end
// MyObject.m private implementation file
@interface MyObject ()
@property (readwrite, copy) NSString *language;
@end
@implementation MyObject
@synthesize language;
@end
我只是想了解上面是否@property
和 @synthesize
关键字生成以下代码:
// MyObject.h public header file
@interface MyObject : NSObject {
NSString *language;
}
-(NSString *)language;
@end
// MyObject.m private implementation file
@interface MyObject ()
-(void)setLanguage: (NSString *) aString;
@end
@implementation MyObject
-(NSString *)language {
return language;
}
-(void)setLanguage: (NSString *) aString {
[language release];
language = [aString copy];
}
@end
因此,编译器会看到第一个 @property
声明并添加一个 getter公共接口中的方法...然后,当涉及到实现文件时,它会找到同一属性的另一个 @property
声明,但在私有接口中具有 readwrite 属性,并且仅添加一个 setter 方法,因为getter 已经添加到 public 中接口..然后,找到@synthesize
关键字,并将两个实现都添加到私有实现部分..第一个@property
声明的复制属性将不是必需的,因为那里不需要 setter,但我们必须指定它以与第二个属性重新声明一致。我的想法对吗?
I've read the Property redeclaration chapter in The Objective-C Programming Language document and I'd like if some of you can clarify me the following property redeclaration:
// MyObject.h public header file
@interface MyObject : NSObject {
NSString *language;
}
@property (readonly, copy) NSString *language;
@end
// MyObject.m private implementation file
@interface MyObject ()
@property (readwrite, copy) NSString *language;
@end
@implementation MyObject
@synthesize language;
@end
I just want to understand if the above @property
and @synthesize
keywords produce the following code:
// MyObject.h public header file
@interface MyObject : NSObject {
NSString *language;
}
-(NSString *)language;
@end
// MyObject.m private implementation file
@interface MyObject ()
-(void)setLanguage: (NSString *) aString;
@end
@implementation MyObject
-(NSString *)language {
return language;
}
-(void)setLanguage: (NSString *) aString {
[language release];
language = [aString copy];
}
@end
So, what happens is that the compiler sees the first @property
declaration and adds a getter method in the public interface... than, when it comes to the implementation file it finds another @property
declaration for the same property but with readwrite attribute within the private interface and adds only a setter method since the getter has been already added to the public interface.. then, the @synthesize
keyword is found and both implementations are added to the private implementation section.. the copy attribute of the first @property
declaration would not be necessary, since the setter is not needed there, but we must specify it to be consistent with the second property redeclaration. Are my thoughts right?
发布评论
评论(1)
是的,你的理解是正确的。
另请注意,Objective-C 中没有严格私有的方法。外部调用者仍然可以调用
setLanguage:
。编译器将输出警告,但该消息将在运行时传递。Yes, your understanding is correct.
Also note that there are no strictly private methods in Objective-C. An external caller can still call
setLanguage:
. The compiler will output a warning but the message would get through at runtime.