- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我了解到,从 iOS 4 开始,现在可以完全不声明 iVar,并允许编译器在您合成属性时自动为您创建它们。但是,我无法从 Apple 找到有关此功能的任何文档。
此外,是否有任何关于最佳实践的文档或 Apple 推荐的关于使用 iVar 和属性的指南?我一直使用这样的属性:
.h文件
@interface myClass {
NSIndexPath *_indexPath
}
@property(nonatomic, retain) NSIndexPath *indexPath
@end
.m文件
@implementation myClass
@synthesize indexPath = _indexPath;
- (void)dealloc {
[_indexPath release];
}
@end
我使用 _indexPath 而不是 indexPath 作为我的 iVar 名称,以确保在我需要使用 self.indexPath
时我永远不会使用 indexPath
。但现在 iOS 支持自动属性,我不需要担心这个。但是,如果我遗漏了 iVar 声明,我应该如何处理在我的 dealloc 中释放它?我被教导在 dealloc 中释放时直接使用 iVars,而不是使用属性方法。如果我在设计时没有 iVar,我可以只调用属性方法吗?
最佳答案
我用过很多不同的方法来处理这个问题。我现在的方法是使用dealloc中的property access。不这样做的理由(在我看来)太过人为而不是不这样做,除非我知道该属性(property)有奇怪的行为。
@interface Class
@property (nonatomic, retain) id prop;
@end
@implementation Class
@synthesize prop;
- (void)dealloc;
{
self.prop = nil;
//[prop release], prop=nil; works as well, even without doing an explicit iVar
[super dealloc];
}
@end
关于objective-c - 带有@synthesize 的自动 iVar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5343475/
在我将一个对象分配给该对象可能已经分配给的 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
我是一名优秀的程序员,十分优秀!