objective-c - 具有自定义 getter 的只读属性是否会合成一个实例变量?

标签 objective-c properties

我有一种情况,我不想为一组属性合成实例变量(我通过用户默认值和钥匙串(keychain)管理它们)。

如果我有以下内容,_loginUsername 肯定会被合成:

@property (nonatomic, readonly) NSString *loginUsername;

而且我知道如果我有以下内容,则不会合成实例变量,因为声明说“我将自己处理它”:

@property (nonatomic, getter = loginUsername, setter = setLoginUsername) NSString *loginUsername;

具有自定义 getter 的只读属性的行为是否相同?

@property (nonatomic, readonly, getter = loginUsername) NSString *loginUsername;

是否会为最终的属性声明合成一个_loginUsername?我很确定不会有,但我正在努力更好地掌握属性属性,所以想确保我没有偏离。

解释这是否在编译时已知的加分项(不要在 Xcode 中作弊和检查!)。

我的实现文件中没有任何@synthesize语句。

最佳答案

Note: The compiler will automatically synthesize an instance variable in all situations where it’s also synthesizing at least one accessor method. If you implement both a getter and a setter for a readwrite property, or a getter for a readonly property, the compiler will assume that you are taking control over the property implementation and won’t synthesize an instance variable automatically. If you still need an instance variable, you’ll need to request that one be synthesized

Programming with Objective-c - Encapsulating data - 在您可以实现自定义访问器方法

关于objective-c - 具有自定义 getter 的只读属性是否会合成一个实例变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22215622/

相关文章:

iphone - Object c property dealloc,哪个是正确的?

c# - C# winforms 中的属性指定资源选择器?

c# - 什么时候使用方法而不是属性来定义类?

objective-c - 默认情况下,属性假定为原子的

ios - 如何在 Objective-C 中旋转 UIButton 和 UILabel 的文本?

ios - 当我不能弱链接时如何测试类的存在

iOS CGAffineTransform Scale 保持左 anchor

objective-c - 将 NSNumber 转换为 NSDecimalNumber

.net - Environment.GetCommandLineArgs - 为什么它是一种方法?为什么不是特性?

javascript - Nodemailer 使用 gmail,无法在字符串 'mailer' 上创建属性 'SMTP'