- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
如果我有一个 @property
而我不想通过 ivar
支持我只是省略了 @synthesize
并且有返回计算值的手动 getter。
但是,从 Xcode 4.4 开始,如果我不指定 @synthesize
,编译器将自动生成它。这是否意味着即使我不需要/不使用它,它也会生成一个 ivar
?
我最终可以使用 dynamic
强制不自动合成。然而,这是错误的,因为如果 getter and setter are implemented somewhere else or during runtime,@dynamic
应该用于关闭警告。 .
最佳答案
在我的工作中,我注意到以下行为。
@synthesize
,有 getter 而没有 setter,那么它将生成 iVar。@synthesize
,没有getter,但有setter,那么它将生成iVar。 @synthesize
并且同时有 getter 和 setter,那么它不会生成 iVar。@synthesize
也没有 getter,那么它将生成 iVar。@synthesize
并且有 getter,那么它不会生成 iVar。据此,我认为一般规则是,如果您没有@synthesize
,并且拥有完全实现该属性所需的所有方法,那么它会被假定为动态的并且不会't 生成 iVar。
无论如何,如果您想确保不生成 iVar,则将其声明为 @dynamic
。
对@dynamic的澄清
来自 Declared Properties在 The Objective-C Programming Language :
You use the @dynamic keyword to tell the compiler that you will fulfill the API contract implied by a property either by providing method implementations directly or at runtime using other mechanisms such as dynamic loading of code or dynamic method resolution.
对我来说,即使您直接实现 getter 和 setter,也可以将属性标记为 @dynamic。
关于iOS自动@synthesize而不创建ivar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12933785/
在我将一个对象分配给该对象可能已经分配给的 ivar 的情况下,首先检查它是否已经分配给该 ivar 有什么好处。 例如,代码 A 相对于代码 B 有什么好处吗? 一个 if (ivar != anO
我一直想知道为什么所有苹果代码示例都使用这样的代码: UINavigationController *aNavigationController = [[UINavigationController
aclass.h @interface aClass : NSObject { NSString *name; } @property (nonatomic, retain) IBOutlet
如果我有一个具有两个属性“a”和“b”的类,并且我有一个这些类实例的数组。创建仅包含“a”元素的数组的最佳方法是什么。 最佳答案 最简单的方法是Key-Value Coding : [yourArra
@property(nonatomic)ivar 和 @property(nonatomic,assign)ivar 相同还是不同? 最佳答案 如 docs 中所述: assign - Specifi
这是后续问题:Difference between self.ivar and ivar? : self.name = @"hello"; 我想知道神奇 setter 方法内部做了什么。所以问题是:任
即使我们不拥有(分配/新建/复制)那个 ivar,我们也必须在 dealloc 中释放 ivar 吗? 例如,这是正确的吗? -(void)dealloc { [_aUIImageIvar r
根据Objective-C runtime reference : ivar_getOffset Returns the offset of an instance variable. ptrdiff
我可以在类之外定义@some_ivar。这个 ivar 属于哪个类,它的范围是什么? 例如,我可以在 example.rb 中说 @var = "Hi" 在 irb 中,我可以要求 example.r
我写了以下 objective-c 类.. @interface thumb_user_info : NSObject { @public // Otherwise I was not able to
毫无疑问,关于 iOS 中的内存管理有大量的信息。在阅读了大量相关内容后,我仍然不清楚某些情况下的“最佳”实践。请我对下面两个示例进行澄清... 我有一个 NSMutableArray,它充当 tab
我必须调用一个重复出现的方法,或者换句话说,该方法将调用自身。 同时我必须有一个方法将使用的控制变量。 首先,我会想到声明一个类似 controlIndex 的 ivar,然后在方法中使用它,如下所示
我正在尝试调用函数,但出现错误: warning: passing argument 1 of 'drawPlot' from incompatible pointer type //call
我在接口(interface)上声明了这样一个ivar: BOOL controllerOK; 我必须在自身位于 block 中的 block 中使用此 ivar。有点像 myBlockl = ^()
我发现很多时候我想要一个合成的只读属性,我只是根据其他变量实现该属性的 getter 方法,而不需要 ivar,例如 (注意:我在界面中定义了 ivars,因为我使用的是 OmniGraffle UM
我正在对 UIToolbar 进行子类化,因为我要在我的整个应用程序中重复使用它。 UIToolbar 使用委托(delegate)协议(protocol): // // UIToolbarCusto
我的应用程序中有一个相当大的 View Controller ,我想通过将一些功能分成几类来清理它。我阅读了有关如何实现类别的内容,并且: #import "StatsVC.h" @interface
当我需要一个私有(private)对象时,我目前使用属性,如下所示: // Class extension in .m file @interface MyClass() @property (str
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: How does an underscore in front of a variable in a coc
我遇到过 Objective-C 代码,它在 .m 文件的 @implementation 行下方声明一个变量,而不是在 .h 文件的 @interface block 中。然后它继续像私有(priv
我是一名优秀的程序员,十分优秀!