为什么我必须写* myPointerVar只是有时在Objective-C? [英] Why do I have to write *myPointerVar only SOMETIMES in Objective-C?

查看:261
本文介绍了为什么我必须写* myPointerVar只是有时在Objective-C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个问题,我还是不明白。

That's an issue I still don't understand.

有时候,我不得不写:

NSString* myVariable;
myVariable = @"Hey!";

然后,例如我定义了一个结构DemoStruct和获取使用它的一个变量。可以说,我有一个具有X和Y瓦尔从double类型的结构。

Then, for example I define a Structure "DemoStruct" and get an Variable that uses it. Lets say I have a Structure that has x and y vars from type double.

我想这个VAR传递给方法,然后操纵我的变种,我想,这手法对我从中通过VAR给方法的上下文效果。所以我需要一个指针,正确的。

I want to pass this var to a method which then manipulates my var, and I want that this manipulation has effect on the context from which I passed the var to that method. So I need a pointer, right.

我把它传递给这样的方法:

I pass it to the method like that:

[someObject someMethod:&myVarThatUsesTheStruct]

这方法现在看起来像:

- (void)someMethod:(DemoStruct*)myVar {
(*myVar).x += 10;
}

通话之前,该结构的组成部分x为可以说现在1000,10,并将它的方法调用之后是1010。

Before the call, the component x of the struct was lets say 1000. Now, 10 is added and it is 1010 after the method call.

但我真的真的很难不明白为什么我要使用星号*为myVar的的方法,因为我说的已经在方法头是myVar的是指向一个DemoStruct。我只是通过与&安培; myVarThatUsesTheStruct的内存地址

But I really really hardly dont get it why I have to use the Asterisk * for myVar in the Method, since I say already in the Method Header that myVar is a POINTER to a DemoStruct. I just pass with &myVarThatUsesTheStruct the memory address.

有人能解释这是为什么喜欢它?

Can someone explain why this is like it is?

推荐答案

正如你所说, myVar的是一个指针。因此,myVar.x是不正确的:它会通过一个指针的领域,它在C / Objective-C的没有意义。

As you say, myVar is a pointer. As such, myVar.x is not correct: it would by a field of a pointer, which has no sense in C/Objective-C.

如果您要访问的变量指向一个指针,必须添加星号: myVar的是一个指针, * myVar的是变量指向 myVar的

If you want to access to the variable pointed to by a pointer, you have to add the asterisk: myVar is a pointer, *myVar is the variable pointed to by myVar.

此外,你的情况,你可以通过编写用C的特殊构造 myVar-> X ,这是完全等同于( * myVar的).X

Moreover, in your case, you can use a special construct of C by writing myVar->x, which is strictly equivalent to (*myVar).x.

这一切是标准的C,不具体到Objective-C的。

All of this is standard C, not specific to Objective-C.

关于您的第一个例子,你没有,因为你改变了指针的值放一个星号,变量不是值: MYVARIABLE 是一个指针到在声明时被分配值的对象。下一条指令(嘿! MYVARIABLE = @ )的指针值的分配: @嘿!是一个指向一个的NSString 对象。该指针(而不是指向的常数的值)的值赋给 MYVARIABLE ,然后指向的对象 @嘿!

About your first example, you don't have to put an asterisk because you change the value of the pointer, not the value of the variable: myVariable is a pointer to an object which at declaration time is assigned the nil value. The next instruction (myVariable = @"Hey!") is an assignment of pointer values: @"Hey!" is a pointer to a NSString object. The value of this pointer (not the value of the pointed constant) is assigned to myVariable, which then points to the object @"Hey!".

是的,这是diffucult跟随,在第一时间...

Yes, this is diffucult to follow at first time...

这篇关于为什么我必须写* myPointerVar只是有时在Objective-C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆