Suppose that i have UIView viewLoading declared in .h. and i do not directly initialize it (in the first code).
假设我在.h中声明了UIView viewLoading。我不直接初始化它(在第一个代码中)。
The first Code.
第一个代码。
UIView *viewLoading2 = [[[UIView alloc] initWithFrame:CGRectMake(75 , 155, 170.0, 170.0)]];
viewLoading = viewLoading2;
[viewLoading2 release]
The second code:
第二个代码:
viewLoading = [[[UIView alloc] initWithFrame:CGRectMake(75 , 155, 170.0, 170.0)]];
The third Code:
第三条代码:
- (void) viewLoad:(UIView *) viewLoading2
{
viewLoading = viewLoading2;
//do i need to retain, alloc, or release something here?
}
-
2In the first code, do i need to release viewLoading in dealloc ? And what happen if i do not declare its property?
2在第一个代码中,我是否需要在dealloc中释放viewLoading?如果我不宣布其财产会怎样?
-
In the second code, does it have same effect from the first code? (need to dealloc or not).
在第二个代码中,它是否与第一个代码具有相同的效果? (需要dealloc与否)。
-
For the third code, does it have same effect from the first code? and what should i do after i code that? (see the comment)
对于第三个代码,它是否与第一个代码具有相同的效果?我编码之后该怎么办? (见评论)
-
Do iPhone Code always need to have release for variable declared in .h? Or only if the variable declared in .h is allocated? if like in the first code, do i need to dealloc viewLoading?
iPhone代码总是需要释放在.h中声明的变量吗?或者只有在.h中声明的变量被分配?如果喜欢在第一个代码中,我是否需要dealloc viewLoading?
-
what is the different between
有什么不同
self.viewloading = viewLoading2;
self.viewloading = viewLoading2;
and
和
viewloading = viewLoading2;
Thanks
谢谢
1 个解决方案
#1
3
In the first example, you allocated the object (once), and you released it (once), so you don't need to do anything else. On the other hand, viewLoading is invalid as soon as you send the release to viewLoading2, so it's not very useful code.
在第一个示例中,您分配了对象(一次),然后释放它(一次),因此您不需要执行任何其他操作。另一方面,只要将发布发送到viewLoading2,viewLoading就无效,因此它不是非常有用的代码。
In the second, you have not released viewLoading yet, so it does need to be done eventually.
在第二种情况下,您尚未发布viewLoading,因此最终需要完成。
In the third, whatever code allocated the object that was passed into this method via the parameter is responsible for releasing it. It should be valid for the duration of this method, but if you're saving it for later use you need to retain it here, then release it when you're done.
在第三种情况下,分配通过参数传递给此方法的对象的任何代码都负责释放它。它应该在此方法的持续时间内有效,但如果您将其保存以供以后使用,则需要将其保留在此处,然后在完成后将其保留。
Edit:
编辑:
I'm not sure I understand your question 4. A declaration in the interface (.h) file is just reserving space for a pointer. It's not an object declaration, so there's no release required until you actually do an object allocation.
我不确定我理解你的问题4.接口(.h)文件中的声明只是为指针保留空间。它不是对象声明,因此在实际进行对象分配之前不需要释放。
self.viewloading = viewLoading2
is using the properties setter method to do the assignment. If the @property statement has "retain" in it, then a retain is done as part of that assignment. `viewloading = viewLoading2" is a straight assignment, no retain.
self.viewloading = viewLoading2使用属性setter方法进行赋值。如果@property语句中包含“retain”,则保留将作为该赋值的一部分完成。 `viewloading = viewLoading2“是一个直接的赋值,没有保留。
#1
3
In the first example, you allocated the object (once), and you released it (once), so you don't need to do anything else. On the other hand, viewLoading is invalid as soon as you send the release to viewLoading2, so it's not very useful code.
在第一个示例中,您分配了对象(一次),然后释放它(一次),因此您不需要执行任何其他操作。另一方面,只要将发布发送到viewLoading2,viewLoading就无效,因此它不是非常有用的代码。
In the second, you have not released viewLoading yet, so it does need to be done eventually.
在第二种情况下,您尚未发布viewLoading,因此最终需要完成。
In the third, whatever code allocated the object that was passed into this method via the parameter is responsible for releasing it. It should be valid for the duration of this method, but if you're saving it for later use you need to retain it here, then release it when you're done.
在第三种情况下,分配通过参数传递给此方法的对象的任何代码都负责释放它。它应该在此方法的持续时间内有效,但如果您将其保存以供以后使用,则需要将其保留在此处,然后在完成后将其保留。
Edit:
编辑:
I'm not sure I understand your question 4. A declaration in the interface (.h) file is just reserving space for a pointer. It's not an object declaration, so there's no release required until you actually do an object allocation.
我不确定我理解你的问题4.接口(.h)文件中的声明只是为指针保留空间。它不是对象声明,因此在实际进行对象分配之前不需要释放。
self.viewloading = viewLoading2
is using the properties setter method to do the assignment. If the @property statement has "retain" in it, then a retain is done as part of that assignment. `viewloading = viewLoading2" is a straight assignment, no retain.
self.viewloading = viewLoading2使用属性setter方法进行赋值。如果@property语句中包含“retain”,则保留将作为该赋值的一部分完成。 `viewloading = viewLoading2“是一个直接的赋值,没有保留。