gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 03:18:56 25 4
gpt4 key购买 nike

我有一种情况,我不想为一组属性合成实例变量(我通过用户默认值和钥匙串(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com