我何时使用Objective-C在iPhone编程中分配和初始化对象?

时间:2021-04-25 19:58:08

Sometimes when I program for the iPhone, I wonder when you have to allocate and initialize objects and when not to. When you are using UI controls, it seems as if you don't have to do so. Is this true and why?

有时当我为iPhone编程时,我想知道什么时候必须分配和初始化对象,何时不要。当您使用UI控件时,似乎您不必这样做。这是真的吗?为什么?

(Assume that they have been declared in the .h of the view controller.)

(假设它们已在视图控制器的.h中声明。)

Example:

label1.text = @"Hello";

vs

label1 = [[UILabel alloc] init];
label1.text = @"Hello";

Is this because I'm using Interface Builder? Would I have to do this if I were to write our my GUI in code?

这是因为我正在使用Interface Builder吗?如果我在代码中编写我的GUI,我是否必须这样做?

2 个解决方案

#1


5  

Your confusion is because of the NIB file - a NIB file is basically a frozen object graph (i.e. a object with children, who has other children, etc). When you load that NIB file, the runtime calls all of the allocs and inits for you, so that they're already created.

您的困惑是因为NIB文件 - NIB文件基本上是一个冻结的对象图(即有孩子的对象,有其他孩子等)。当您加载该NIB文件时,运行时会为您调用所有的alloc和inits,以便它们已经创建。

When you want to create an object that hasn't been previously specified in the NIB file, that's when you need alloc/init.

如果要创建先前未在NIB文件中指定的对象,则需要alloc / init时。

#2


2  

You basically need to alloc/init ALL objects except for static strings, as above. Even when you're using convenience methods, such as +[NSString stringWithFormat:...], behind the scenes an alloc and init is still occurring. These convenience methods usually just do the alloc and init, and then toss in an -autorelease as well so that you don't have to worry about cleaning up.

你基本上需要分配/初始化除静态字符串之外的所有对象,如上所述。即使您正在使用便利方法,例如+ [NSString stringWithFormat:...],在幕后仍然会发生alloc和init。这些便捷方法通常只需要执行alloc和init,然后再输入-autorelease,这样您就不必担心清理了。

If you're just creating a temporary object, and there's a convenience method that fits, use it. If you want your object to stay around and there's convenience method, usually it's fine to call it and add a -retain, or just use alloc/init.

如果您只是创建一个临时对象,并且有一个适合的便利方法,请使用它。如果你想让你的对象留在那里并且有方便的方法,通常可以调用它并添加-retain,或者只使用alloc / init。

Obviously, if there's no convenience method, use alloc/init.

显然,如果没有方便的方法,请使用alloc / init。

#1


5  

Your confusion is because of the NIB file - a NIB file is basically a frozen object graph (i.e. a object with children, who has other children, etc). When you load that NIB file, the runtime calls all of the allocs and inits for you, so that they're already created.

您的困惑是因为NIB文件 - NIB文件基本上是一个冻结的对象图(即有孩子的对象,有其他孩子等)。当您加载该NIB文件时,运行时会为您调用所有的alloc和inits,以便它们已经创建。

When you want to create an object that hasn't been previously specified in the NIB file, that's when you need alloc/init.

如果要创建先前未在NIB文件中指定的对象,则需要alloc / init时。

#2


2  

You basically need to alloc/init ALL objects except for static strings, as above. Even when you're using convenience methods, such as +[NSString stringWithFormat:...], behind the scenes an alloc and init is still occurring. These convenience methods usually just do the alloc and init, and then toss in an -autorelease as well so that you don't have to worry about cleaning up.

你基本上需要分配/初始化除静态字符串之外的所有对象,如上所述。即使您正在使用便利方法,例如+ [NSString stringWithFormat:...],在幕后仍然会发生alloc和init。这些便捷方法通常只需要执行alloc和init,然后再输入-autorelease,这样您就不必担心清理了。

If you're just creating a temporary object, and there's a convenience method that fits, use it. If you want your object to stay around and there's convenience method, usually it's fine to call it and add a -retain, or just use alloc/init.

如果您只是创建一个临时对象,并且有一个适合的便利方法,请使用它。如果你想让你的对象留在那里并且有方便的方法,通常可以调用它并添加-retain,或者只使用alloc / init。

Obviously, if there's no convenience method, use alloc/init.

显然,如果没有方便的方法,请使用alloc / init。