“->”(箭头操作符)和“。”之间的区别是什么?(点算符)在Objective-C中?

时间:2021-10-11 22:29:46

In Objective-C what is the difference between accessing a variable in a class by using -> (arrow operator) and . (dot operator) ? Is -> used to access directly vs dot (.) isn't direct?

在Objective-C中,使用->(箭头操作符)访问类中的变量和使用。(点操作符)?使用->直接访问vs . dot(.)不是直接访问吗?

3 个解决方案

#1


19  

-> is the traditional C operator to access a member of a structure referenced by a pointer. Since Objective-C objects are (usually) used as pointers and an Objective-C class is a structure, you can use -> to access its members, which (usually) correspond to instance variables. Note that if you’re trying to access an instance variable from outside the class then the instance variable must be marked as public.

->是传统的C操作符,用于访问指针引用的结构的成员。由于Objective-C对象(通常)被用作指针,而Objective-C类是一个结构,您可以使用->访问它的成员,这些成员(通常)对应于实例变量。注意,如果试图从类外部访问实例变量,那么必须将实例变量标记为public。

So, for example:

举个例子:

SomeClass *obj = …;
NSLog(@"name = %@", obj->name);
obj->name = @"Jim";

accesses the instance variable name, declared in SomeClass (or one of its superclasses), corresponding to the object obj.

访问在SomeClass(或它的一个超类)中声明的实例变量名称,与对象obj对应。

On the other hand, . is (usually) used as the dot syntax for getter and setter methods. For example:

另一方面,(通常)用作getter和setter方法的点语法。例如:

SomeClass *obj = …;
NSLog(@"name = %@", obj.name);

is equivalent to using the getter method name:

等于使用getter方法名:

SomeClass *obj = …;
NSLog(@"name = %@", [obj name]);

If name is a declared property, it’s possible to give its getter method another name.

如果name是一个声明的属性,那么可以给它的getter方法另一个名称。

The dot syntax is also used for setter methods. For example:

点语法也用于setter方法。例如:

SomeClass *obj = …;
obj.name = @"Jim";

is equivalent to:

等价于:

SomeClass *obj = …;
[obj setName:@"Jim"];

#2


9  

The arrow, ->, is a shorthand for a dot combined with a pointer dereference, these two are the same for some pointer p:

这个箭头,>,是一个点和一个指针去参照的简写,这两个对于一些指针p是一样的:

p->m
(*p).m

The arrow notation is inherited from C and C has it because the structure member accessing operator (.) binds looser than the pointer dereferencing operator (*) and no one wants to write (*p).m all the time nor do they want to change the operator precedence to make people write *(p.m) to dereference a pointer inside a structure. So, the arrow was added so that you could do both p->m and *s.p sensibly without the ugliness of the parentheses.

箭头符号是从C和C继承而来的,因为结构成员访问操作符(.)绑定比指针引用操作符(*)更松散,没有人想写(*p)。m一直都不希望改变操作符的优先级,让人们编写*(p.m.)来撤销结构中指针的引用。加上箭头,你可以同时做p->m和*s。p明智地没有圆括号的丑陋。

#3


0  

When you use the arrow operator ptr->member it's implicitly dereferencing that pointer. It's equivalent to (*ptr).member. When you send messages to an object pointer, the pointer is implicitly dereferenced as well.

当您使用箭头操作符ptr->成员时,它将隐式地取消该指针的引用。它等于.member(* ptr)。当您向对象指针发送消息时,指针也被隐式地取消引用。

#1


19  

-> is the traditional C operator to access a member of a structure referenced by a pointer. Since Objective-C objects are (usually) used as pointers and an Objective-C class is a structure, you can use -> to access its members, which (usually) correspond to instance variables. Note that if you’re trying to access an instance variable from outside the class then the instance variable must be marked as public.

->是传统的C操作符,用于访问指针引用的结构的成员。由于Objective-C对象(通常)被用作指针,而Objective-C类是一个结构,您可以使用->访问它的成员,这些成员(通常)对应于实例变量。注意,如果试图从类外部访问实例变量,那么必须将实例变量标记为public。

So, for example:

举个例子:

SomeClass *obj = …;
NSLog(@"name = %@", obj->name);
obj->name = @"Jim";

accesses the instance variable name, declared in SomeClass (or one of its superclasses), corresponding to the object obj.

访问在SomeClass(或它的一个超类)中声明的实例变量名称,与对象obj对应。

On the other hand, . is (usually) used as the dot syntax for getter and setter methods. For example:

另一方面,(通常)用作getter和setter方法的点语法。例如:

SomeClass *obj = …;
NSLog(@"name = %@", obj.name);

is equivalent to using the getter method name:

等于使用getter方法名:

SomeClass *obj = …;
NSLog(@"name = %@", [obj name]);

If name is a declared property, it’s possible to give its getter method another name.

如果name是一个声明的属性,那么可以给它的getter方法另一个名称。

The dot syntax is also used for setter methods. For example:

点语法也用于setter方法。例如:

SomeClass *obj = …;
obj.name = @"Jim";

is equivalent to:

等价于:

SomeClass *obj = …;
[obj setName:@"Jim"];

#2


9  

The arrow, ->, is a shorthand for a dot combined with a pointer dereference, these two are the same for some pointer p:

这个箭头,>,是一个点和一个指针去参照的简写,这两个对于一些指针p是一样的:

p->m
(*p).m

The arrow notation is inherited from C and C has it because the structure member accessing operator (.) binds looser than the pointer dereferencing operator (*) and no one wants to write (*p).m all the time nor do they want to change the operator precedence to make people write *(p.m) to dereference a pointer inside a structure. So, the arrow was added so that you could do both p->m and *s.p sensibly without the ugliness of the parentheses.

箭头符号是从C和C继承而来的,因为结构成员访问操作符(.)绑定比指针引用操作符(*)更松散,没有人想写(*p)。m一直都不希望改变操作符的优先级,让人们编写*(p.m.)来撤销结构中指针的引用。加上箭头,你可以同时做p->m和*s。p明智地没有圆括号的丑陋。

#3


0  

When you use the arrow operator ptr->member it's implicitly dereferencing that pointer. It's equivalent to (*ptr).member. When you send messages to an object pointer, the pointer is implicitly dereferenced as well.

当您使用箭头操作符ptr->成员时,它将隐式地取消该指针的引用。它等于.member(* ptr)。当您向对象指针发送消息时,指针也被隐式地取消引用。