- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在实现接口(interface)时,教程和文献中的常见方法似乎是声明一个 ivar,然后设置 @property
,然后设置 @synthesize
。
@interface MyClass : NSObject {
NSString *myString;
}
@property (nonatomic, retain) NSString *myString;
@end
但是,省略显式声明并仅放置@property 具有相同的效果。
@interface MyClass: NSObject {
}
@property (nonatomic, retain) NSString *myString;
@end
那么为什么大多数人会使用 @property
和显式声明呢?不这样做是不好的形式吗?
最佳答案
它曾经是必要的。有两个不同版本的 Objective-C 运行时:仅 32 位的“传统”运行时(旧版本)和 32/64 位运行时(新的 32 位运行时仅用于 iOS 设备和iOS 模拟器)。
我认为唯一仍然需要的地方是当您在 32 位模式(10.5 或 10.6)下运行应用程序时。其他任何地方(64 位 Leopard、64 位雪豹、Lion、iOS)都使用具有“自动 ivar 合成”的较新运行时,生成的 ivar 称为“合成 ivar”。
关于objective-c - Objective C : Why do we declare ivars in the . h 成员(member)区 if @property 好像是自动做的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5555736/
在我将一个对象分配给该对象可能已经分配给的 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
我是一名优秀的程序员,十分优秀!