本文介绍了("")点运算符和箭头(" - >")运算符的使用使用C与Objective-C的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图环绕的一些使用C与Objective-C中的用法和语法的差异我的头。我特别想知道如何使用(为什么)不同的点运算符和使用C与Objective-C的箭头运营商。下面是一个简单的例子。

I'm trying to wrap my head around some of the differences in usage and syntax in C vs. Objective-C. In particular, I want to know how (and why) the usage differs for the dot operator and the arrow operator in C vs. Objective-C. Here is a simple example.

C code:

// declare a pointer to a Fraction
struct Fraction *frac;

...

// reference an 'instance' variable
int n = (*frac).numerator;     // these two expressions
int n = frac->numerator;       // are equivalent

的Objective-C code:

// declare a pointer to a Fraction
Fraction *frac = [[Fraction alloc] init];

...

// reference an instance variable
int n = frac.numerator;        // why isn't this (*frac).numerator or frac->numerator??

所以,看到压裂是如何在这两个程序是相同的(即它是一个指向一个Fraction对象或结构),他们为什么要访问属性时使用不同的语法?特别是,在C,在分子属性与访问压裂>分子,而是用Objective-C的,它使用点运算符访问,与 frac.numerator 。由于压裂是这两个程序的指针,为什么这些前pressions有什么不同?谁能帮助澄清这对我?

So, seeing how frac is the same in both programs (i.e. it is a pointer to a Fraction object or struct), why are they using different syntax when accessing properties? In particular, in C, the numerator property is accessed with frac->numerator, but with Objective-C, it is accessed using the dot operator, with frac.numerator. Since frac is a pointer in both programs, why are these expressions different? Can anyone help clarify this for me?

推荐答案

压裂其实并没有这两个程序是相同的。

frac is actually not the same in both programs.

AC 分数结构,这是一个基本类型,没有超载运营商和是唯一真正能够构建并默认破坏。如果你定义的结构功能或领域,方式访问 C 是用点(这些属性)运营商。当您使用结构的宗旨-C保持这个操作符。为方便起见,您可以执行使用箭头取消引用和点操作 - (你所提到的两个等价的前pressions)运算符(&GT )。 Objective-C的也preserves这个访问时,结构秒。

A C Fraction is a struct, which is a base type with no overloaded operators and is only really able to be constructed and destructed by default. If you define functions or fields on the struct, the way to access those properties in C is with the dot (.) operator. Objective-C maintains this operator when you use structs. For convenience, you can perform a dereference-and-dot operation using the arrow (->) operator (the two equivalent expressions you mention). Objective-C also preserves this when accessing structs.

一个Objective-C 分数在你的榜样,但是,可能是(一个假设的)至少键入 ID ,这是一个简单的类名和指针引擎盖下的类的实例。这也很可能是 NSObject的的子类或 NSProxy 。这些Objective-C类是特殊的,因为它们有predefined操作上只是一个 C 结构之上的整层(如果你真的想钻研它,然后你可以看看的Objective-C运行时参考)。另外需要注意,一个Objective-C类的总是指向

An Objective-C Fraction in your example, however, is probably (one would assume) a pointer of at least type id, which is simply a classname and pointer to the instance of that class under the hood. It's also very likely to be a subclass of NSObject or NSProxy. These Objective-C classes are special in that they have a whole layer of predefined operations on top of just a C struct (if you really want to dig into it then you can take a look at the Objective-C Runtime Reference). Also important to note, an Objective-C class is always a pointer.

其中最基本操作是<$c$c>objc_msgSend.当我们对这些类型的对象进行操作,Objective-C的编译器间$ P $点一个点()运算符或方括号语法( [对象的方法] )为 objc_msgSend 方法调用。有关实际发生在这里的更多详细信息,请参见的帖子。

One of the most basic operations is objc_msgSend. When we operate on these types of objects, the Objective-C compiler interprets a dot (.) operator or the square bracket syntax ([object method]) as an objc_msgSend method call. For more detailed info about what actually happens here, see this series of posts by Bill Bumgarner, an Apple engineer who oversees the development of the Obj-C runtime.

箭头( - &GT; )操作符是不是真的应该是对Objective-C的对象使用。就像我说的,Objective-C类的实例是一个C结构与通信添加额外的一层,但是当你使用箭头通信的那层基本上绕过。例如,如果你打开​​X code和键入 [UIApplication的sharedApplication] - &GT; ,然后调出方法完成列表,可以看到这一点:

The arrow (->) operator is not really supposed to be used on Objective-C objects. Like I said, Objective-C class instances are a C struct with an extra layer of communication added, but that layer of communication is essentially bypassed when you use the arrow. For example, if you open up Xcode and type in [UIApplication sharedApplication]-> and then bring up the method completion list, you see this:

在这里你可以看到一堆我们通常访问与方括号语法正常的字段(如 [UIApplication的sharedApplication]代表] )。这些具体的项目,但是,是存储各自的Objective-C的属性值的 C 字段。

Here you can see a bunch of normal fields which we generally access with square bracket syntax (like [[UIApplication sharedApplication] delegate]). These particular items, however, are the C fields that store the values of their respective Objective-C properties.

所以,你可以粗略地认为它是这样的:

So, you can roughly think of it like this:

一个C对象上点运算符


  1. (在运行时)字段的返回值

一个C对象(指针)上的箭头操作符


  1. 取消引用指针

  2. 字段的返回值

点运算符/方括号中的Objective-C对象(指针)


  1. (在编译时)与调用替换 objc_msgSend

  2. (在运行时)查找对象 - 类定义,抛出异常,如果出事了

  3. 取消引用指针

  4. 字段的返回值

一个Objective-C的对象(指针)上的箭头操作符


  1. (在运行时)取消引用指针

  2. 字段的返回值

现在我肯定在这里简单化,但总结:箭头运营商似乎基本上做同样的事情在这两种情况下,但点操作员在Objective-C的额外/不同的含义。

Now I'm definitely oversimplifying here, but to summarise: the arrow operators appear to do basically the same thing in both cases, but the dot operator has an extra/different meaning in Objective-C.

这篇关于(&QUOT;&QUOT;)点运算符和箭头(&QUOT; - &GT;&QUOT;)运算符的使用使用C与Objective-C的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 05:15