内的初始化方法,我有以下的代码非保留对象:何时被释放?
- (id)init {
self = [super init];
if (self) {
UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
tempButton.frame = CGRectMake(0,0,300,44);
// some custom code...
self.myButton = tempButton;
}
return self;
}
凡myButton
是保留的财产。 我知道,对于所关注的内存管理规则,这种方法等于这个其他:
- (id)init {
self = [super init];
if (self) {
UIButton *tempButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,300,44)];
// some custom code...
self.myButton = tempButton;
[tempButton release];
}
return self;
}
但在这种情况下,我需要使用的第一个“版本”,因为buttonType
属性为只读后,我不能改变它使按钮失去活力。
因为我发现自己在我的应用程序和多个对象(其中大部分是NSString
)的多个方法中使用“非init-release”版本,我的问题是:不计算在这种情况下,分配给属性保留的对象,何时tempButton
对象将被释放?也许在方法/ if语句的末尾?或者,第一个“版本”会导致内存使用量增加,因为该对象不会立即被释放,而是在一段时间之后?
唉,我错过了运行循环部分。你有可能对“运行循环的当前迭代”更精确吗?那个迭代与单一方法或更长的时间相关,也许是固定的时间?可能是整个应用程序的生命周期?我试着通过阅读教程来了解更多,但没有人明确地解释这一点。 – 2012-02-15 00:19:02
“运行循环的当前迭代”实际上与我所能达到的一样精确。运行循环是Cocoa中的事件处理循环。如果您想了解更多信息,请点击以下博客文章:http://blog.shinetech.com/2009/06/02/run-loops-vs-threads-in-cocoa/ – yuji 2012-02-15 00:26:12