Readonly public property redeclared as readwrite in private interface.. understanding a bit more

∥☆過路亽.° 提交于 2019-12-06 09:12:09

问题


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:


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.



来源:https://stackoverflow.com/questions/7566384/readonly-public-property-redeclared-as-readwrite-in-private-interface-understa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!