如何在Objective C中声明类的实例?

时间:2023-01-15 15:13:03

Let's imagine I have Fraction class. So, the correct way to create instance of it is:

我们假设我有Fraction类。因此,创建它的实例的正确方法是:

Fraction *myFraction;
or
myFraction = Fraction;
or
myFraction = [Fraction new];

or something else?

或者是其他东西?

In the book i'm studying the correct one is first, but it looks unreasonable to me. Why do we have to create a pointer for it? Why don't we make the real instance?

在书中我正在研究正确的一个是第一个,但这对我来说看起来不合理。为什么我们必须为它创建一个指针?为什么我们不做真实的实例?

That first expression means - give me a pointer to the new instance of Fraction class, doesn't it?

第一个表达式意味着 - 给我一个指向Fraction类的新实例的指针,不是吗?

3 个解决方案

#1


4  

The first declares a variable named myFraction of type Fraction *, but doesn't create anything, nor initialize myFraction. The second isn't valid. The third creates a new Fraction and assigns it to a previously declared variable named myFraction. Often in Objective-C, you'll declare and initialize a variable in a single statement:

第一个声明了一个名为myFraction的Fraction *类型的变量,但是没有创建任何东西,也没有初始化myFraction。第二个无效。第三个创建一个新的Fraction并将其分配给先前声明的名为myFraction的变量。通常在Objective-C中,您将在单个语句中声明和初始化变量:

Fraction *myFraction = [[Fraction alloc] init];

As for whether to use new or alloc followed by init, it's largely a matter of taste.

至于是否使用new或者alloc是init,它主要是品味问题。

Variables for storing objects are pointers in part because Objective-C inherited C's call-by-value semantics. When one variable is assigned to another (such as when passing it to a function), the value will be copied. At best, this is inefficient for immutable objects. At worst, it leads to incorrect behavior. Pointers are a way around call-by-value and the copy-on-assign semantics: the value of a variable with pointer type is just the pointer. It can be copied without touching the target object. The cost for this is you need some form of memory management.

用于存储对象的变量部分是因为Objective-C继承了C的按值调用语义。将一个变量分配给另一个变量时(例如将其传递给函数时),将复制该值。充其量,这对于不可变对象来说效率低下。最糟糕的是,它会导致不正确的行为。指针是一种绕过值调用和分配时复制语义的方法:具有指针类型的变量的值只是指针。可以在不触摸目标对象的情况下复制它。这样做的代价是你需要某种形式的内存管理。

#2


3  

It would be a good idea to read Kernihan and Ritchie's "The C Programming Language" so you can get an idea about how variables are declared.

阅读Kernihan和Ritchie的“C编程语言”是一个好主意,这样你就可以了解变量的声明方式。

There are two modes of allocation in C and Obj-C and C++: manual and automatic.

C和Obj-C和C ++有两种分配方式:手动和自动。

Integers and floats and characters and such are generally automatically declared. They are created when the declaration passes (i.e. int i), and deleted when the scope they were created in goes away, i.e. when you exit the block in which they were declared. They're called automatics. (it's also possible to declare them "static" but for the purposes of this discussion regarding allocation, these are the same)

通常自动声明整数和浮点数以及字符等。它们是在声明传递时创建的(即int i),并在创建它们的范围消失时删除,即当你退出声明它们的块时。他们被称为自动化。 (也可以将它们声明为“静态”但是为了讨论分配的目的,这些是相同的)

Objects are too complicated to pass around to functions, as function parameters are "pass by value", meaning that the parameter gets a copy of the value being passed in, instead of the variable itself. It'd take a huge amount of time to copy a whole object all the time.

对象太复杂而无法传递给函数,因为函数参数是“按值传递”,这意味着参数获取传入的值的副本,而不是变量本身。复制整个对象需要花费大量时间。

For this reason, you want to just tell the various functions where they can find the object. Instead of handing off a copy of the object, you hand off a copy of the address of the object. The address is stored in an automatic with a type of pointer. (This is really just an integer, but it's size is dictated by the hardware and OS, so it needs to be a special type.)

因此,您只想告诉他们可以找到对象的各种功能。您不需要交出对象的副本,而是交出对象地址的副本。地址存储在带有指针类型的自动中。 (这实际上只是一个整数,但它的大小由硬件和操作系统决定,所以它需要是一种特殊的类型。)

The declaration Fraction *myFraction; means "myFraction is a pointer, and just so you know, it's going to point to a Fraction later."

声明Fraction * myFraction;意思是“myFraction是一个指针,只是你知道,它将在稍后指向一个Fraction。”

This will automatically allocate the pointer, but not the whole Fraction. For that to happen, you must call alloc and init.

这将自动分配指针,但不会分配整个分数。为此,您必须调用alloc和init。

The big reason why you have this two step process is that since we typically want objects to stick around for a while, we don't want the system automatically killing them at the end of a function. We need them to persist. We create places to hang the object in our functions, but those hangers go away when they aren't needed. We don't want them taking the object with them.

你有这两步过程的一个重要原因是,由于我们通常希望对象停留一段时间,我们不希望系统在函数结束时自动杀死它们。我们需要他们坚持下去。我们创建了将对象悬挂在我们的功能中的地方,但是当不需要时,这些衣架就会消失。我们不希望他们带对象。

Ultimately, you might make declarations like this:

最终,您可能会做出如下声明:

Fraction *myFraction = [[Fraction alloc] initWithNumerator: 2 Denominator: 3];

which says: "Make me a Fraction, and set it to be 2/3, and then put the address of that Fraction into 'myFraction'."

其中说:“让我成为一个分数,并将其设置为2/3,然后将该分数的地址放入'myFraction'。”

#3


0  

Why do we have to create a pointer for it? Why don't we make the real instance?

为什么我们必须为它创建一个指针?为什么我们不做真实的实例?

In Objective-C, every object is pointer type. So, you need to use either new or alloc/init.

在Objective-C中,每个对象都是指针类型。因此,您需要使用new或alloc / init。

Fraction *myFraction = [ Fraction new ] ;

or

Fraction *myFraction = [ [Fraction alloc] init ] ;

And myFraction needs to be released.

myFraction需要发布。

That first expression means - give me a pointer to the new instance of Fraction class, doesn't it?

第一个表达式意味着 - 给我一个指向Fraction类的新实例的指针,不是吗?

No, you are just declaring a pointer of type Fraction. And the second statement is not even valid.

不,您只是声明了Fraction类型的指针。而第二个声明甚至没有效。

#1


4  

The first declares a variable named myFraction of type Fraction *, but doesn't create anything, nor initialize myFraction. The second isn't valid. The third creates a new Fraction and assigns it to a previously declared variable named myFraction. Often in Objective-C, you'll declare and initialize a variable in a single statement:

第一个声明了一个名为myFraction的Fraction *类型的变量,但是没有创建任何东西,也没有初始化myFraction。第二个无效。第三个创建一个新的Fraction并将其分配给先前声明的名为myFraction的变量。通常在Objective-C中,您将在单个语句中声明和初始化变量:

Fraction *myFraction = [[Fraction alloc] init];

As for whether to use new or alloc followed by init, it's largely a matter of taste.

至于是否使用new或者alloc是init,它主要是品味问题。

Variables for storing objects are pointers in part because Objective-C inherited C's call-by-value semantics. When one variable is assigned to another (such as when passing it to a function), the value will be copied. At best, this is inefficient for immutable objects. At worst, it leads to incorrect behavior. Pointers are a way around call-by-value and the copy-on-assign semantics: the value of a variable with pointer type is just the pointer. It can be copied without touching the target object. The cost for this is you need some form of memory management.

用于存储对象的变量部分是因为Objective-C继承了C的按值调用语义。将一个变量分配给另一个变量时(例如将其传递给函数时),将复制该值。充其量,这对于不可变对象来说效率低下。最糟糕的是,它会导致不正确的行为。指针是一种绕过值调用和分配时复制语义的方法:具有指针类型的变量的值只是指针。可以在不触摸目标对象的情况下复制它。这样做的代价是你需要某种形式的内存管理。

#2


3  

It would be a good idea to read Kernihan and Ritchie's "The C Programming Language" so you can get an idea about how variables are declared.

阅读Kernihan和Ritchie的“C编程语言”是一个好主意,这样你就可以了解变量的声明方式。

There are two modes of allocation in C and Obj-C and C++: manual and automatic.

C和Obj-C和C ++有两种分配方式:手动和自动。

Integers and floats and characters and such are generally automatically declared. They are created when the declaration passes (i.e. int i), and deleted when the scope they were created in goes away, i.e. when you exit the block in which they were declared. They're called automatics. (it's also possible to declare them "static" but for the purposes of this discussion regarding allocation, these are the same)

通常自动声明整数和浮点数以及字符等。它们是在声明传递时创建的(即int i),并在创建它们的范围消失时删除,即当你退出声明它们的块时。他们被称为自动化。 (也可以将它们声明为“静态”但是为了讨论分配的目的,这些是相同的)

Objects are too complicated to pass around to functions, as function parameters are "pass by value", meaning that the parameter gets a copy of the value being passed in, instead of the variable itself. It'd take a huge amount of time to copy a whole object all the time.

对象太复杂而无法传递给函数,因为函数参数是“按值传递”,这意味着参数获取传入的值的副本,而不是变量本身。复制整个对象需要花费大量时间。

For this reason, you want to just tell the various functions where they can find the object. Instead of handing off a copy of the object, you hand off a copy of the address of the object. The address is stored in an automatic with a type of pointer. (This is really just an integer, but it's size is dictated by the hardware and OS, so it needs to be a special type.)

因此,您只想告诉他们可以找到对象的各种功能。您不需要交出对象的副本,而是交出对象地址的副本。地址存储在带有指针类型的自动中。 (这实际上只是一个整数,但它的大小由硬件和操作系统决定,所以它需要是一种特殊的类型。)

The declaration Fraction *myFraction; means "myFraction is a pointer, and just so you know, it's going to point to a Fraction later."

声明Fraction * myFraction;意思是“myFraction是一个指针,只是你知道,它将在稍后指向一个Fraction。”

This will automatically allocate the pointer, but not the whole Fraction. For that to happen, you must call alloc and init.

这将自动分配指针,但不会分配整个分数。为此,您必须调用alloc和init。

The big reason why you have this two step process is that since we typically want objects to stick around for a while, we don't want the system automatically killing them at the end of a function. We need them to persist. We create places to hang the object in our functions, but those hangers go away when they aren't needed. We don't want them taking the object with them.

你有这两步过程的一个重要原因是,由于我们通常希望对象停留一段时间,我们不希望系统在函数结束时自动杀死它们。我们需要他们坚持下去。我们创建了将对象悬挂在我们的功能中的地方,但是当不需要时,这些衣架就会消失。我们不希望他们带对象。

Ultimately, you might make declarations like this:

最终,您可能会做出如下声明:

Fraction *myFraction = [[Fraction alloc] initWithNumerator: 2 Denominator: 3];

which says: "Make me a Fraction, and set it to be 2/3, and then put the address of that Fraction into 'myFraction'."

其中说:“让我成为一个分数,并将其设置为2/3,然后将该分数的地址放入'myFraction'。”

#3


0  

Why do we have to create a pointer for it? Why don't we make the real instance?

为什么我们必须为它创建一个指针?为什么我们不做真实的实例?

In Objective-C, every object is pointer type. So, you need to use either new or alloc/init.

在Objective-C中,每个对象都是指针类型。因此,您需要使用new或alloc / init。

Fraction *myFraction = [ Fraction new ] ;

or

Fraction *myFraction = [ [Fraction alloc] init ] ;

And myFraction needs to be released.

myFraction需要发布。

That first expression means - give me a pointer to the new instance of Fraction class, doesn't it?

第一个表达式意味着 - 给我一个指向Fraction类的新实例的指针,不是吗?

No, you are just declaring a pointer of type Fraction. And the second statement is not even valid.

不,您只是声明了Fraction类型的指针。而第二个声明甚至没有效。