只读公共属性在私有接口中重新声明为可读写..了解更多

发布于 2024-12-06 20:07:39 字数 1406 浏览 1 评论 0 原文

我已阅读 属性重新声明章节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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明月夜 2024-12-13 20:07:39

是的,你的理解是正确的。

另请注意,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文