指针 VS 变量,Objective-C [英] Pointers VS Variables, Objective-C

查看:64
本文介绍了指针 VS 变量,Objective-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Objective-C 的初学者,我无法理解指针和变量之间的区别.比如

I am a beginner in Objective-C and I am having trouble grasping the difference between pointers and variables. For example, what is difference between

int number = 30;

int number = 30;

int *pointerNumber = 30;

int *pointerNumber = 30;

如果有人能向我解释这一点,我将不胜感激.

I would be really grateful if someone could explain this to me.

推荐答案

指针只是句柄、位置或内存地址.所以你用一个指针,你不是直接操作一个东西,你是在操作指针指向的内存中的东西.

A pointer is simply a handle, location, or memory address. So you with a pointer, you're not directly manipulating a thing, you're manipulating something in memory at which the pointer points.

但是,对于像 int 这样的标量值,您可以直接使用该数据.一种给定的语言如何使用指针和值取决于该语言,因此它们的存储位置、访问方式等都会产生影响.

However, with a scalar value like an int, you are working directly with that piece of data. How a given language uses pointers and values is up to the language, so there are implications as to where they are stored, how they are accessed, etc.

这不是故事的全部,但恕我直言,这是解释它的最简单方法.

That's not the whole story, but it's the easiest way to explain it IMHO.

现在,在 Objective-C 的上下文中,您不会真正找到很多指向 int 类型的指针,您会找到一个 int 值.另一方面,对象疯狂地使用指针,它允许您将对象传递给方法,或者在消息中发送它,修改它,并且仍然拥有相同的对象,只是发生了变化.

Now, in the context of Objective-C, you wouldn't really find many pointers to an int type, you would find an int value. Objects on the other hand use pointers like crazy, and it's what allows you to pass around an Object to a method, or send it in a message, modify it, and still have the same object, just mutated.

但是,使用该 int,将其作为消息参数传递肯定会传递 int 的值,但不会传递对它的引用,因此原始 int 将保持不变.

However, with that int, passing it as a message parameter will surely pass the value of the int, but not a reference to it, so the original int will remain intact.

这里有一些关于指针

这是Objective-C中的一个例子来指出这一点

And here is an example in Objective-C to point it out

让我们暂时假设我们有一个简单的 iOS 或 OSX 应用程序,遵循 Apple 的 Objective-C 标准和库.我们还假设我们有一个类 Foo,带有适当定义的标题

Let's assume for a moment we have a simple iOS or OSX app following Apple's Objective-C standards and libraries. Let's also assume we have a class, Foo, with the appropriately defined header

#import Foo.h
#import Bar.h
@implementation Foo
    - (void) doSomethingWithPointer:(Bar)obj{
        obj.x = 5;
    }

    - (void) doSomethingWithValue:(int)num{
        int x = num;
    }

    - (void) doThings:{
    //Suppose I have an object of type Bar available to me. I'm going to get a pointer to a new instance of that object, freshly allocated into memory
    Bar *barObj = [Bar alloc];
    //we will assume Bar has only one feature, a publicly accessible property called x. It's of the int type, of value 1, but again, is part of the Bar object that we are POINTING to.

    //Now, here is a variable that is NOT a POINTER, it's merely a value in memory.
    int num = 1;

    //Let's call the two methods in this class that perform some action and see what happens.

    //first, our pointer
    NSLog(@"%d",barObj.x);//outputs 1
    [self doSomethingWithPointer:barObj];
    NSLog(@"%d",barObj.x);//outputs 5

    //Now, let's call on our variable that is just a  value and NOT a POINTER
    NSLog(@"%d",num);//outputs 1
    [self doSomethingWithValue:num];
    NSLog(@"%d",num);//outputs 1

}
@end

我在这里演示的不仅仅是指针的概念,还有通过引用传递的概念.除此之外,我还展示了指针和由变量表示的标量值之间的区别.当我们将变量"(如您所说的那样)传递给方法时,它会在方法中进行修改,但正如您所看到的,原始内容保持原样,并且在方法中使用的内容之后没有更改.只传递了值,而不是对原始值的引用.

What I have demonstrated here is not just the concept of a pointer, but also of passing by reference. Aside from that, I have also shown what the difference is between a pointer and a scalar value represented by a variable. When we passed "variable", as you call it, to a method, it's modified in the method, but as you can see the original was kept in tact and did not change after what was used in the method was. Only the value was passed and not the reference to that original value.

然而,在对象的情况下,我们传递了一个指向对象的指针.由于我们修改了该点表示的对象,在内存中的那个位置,对象内部的值发生了变化,因为我们本质上是在同一个对象上操作.我们知道在内存中的何处寻找它.

However, in the case of the object, we passed a pointer to the object. Since we modified the object represented by that point, at THAT location in memory, the value inside the object DID change because we were in essence acting on the same object. We knew where to look for it in memory.

如果这仍然没有意义,请在评论中告诉我,我们会解决这个问题:)

If this is still not making sense just let me know in the comments and we'll figure this thing out :)

如果您不太了解指针,我将假设您来自脚本或 Java 背景;)

I'm going to assume you come from either a scripting or Java background if you don't know pointers that well ;)

作为旁注,为什么只有 obj-c 中的对象才会出现这种情况?对象,或者更确切地说是类的实例,封装数据,并且该数据需要能够保持完整并传递,但是如果发生变异,无论对象的相同实例在哪里,该变异也需要反映在对象的状态中用过.

As a side note, why is this the case only with objects in obj-c it would seem? Objects, or rather instances of Classes, encapsulate data, and that data needs to be able to remain intact and passed around, but if mutated, that mutation needs to also be reflected in the object's state no matter where that same instance of the object is used.

这篇关于指针 VS 变量,Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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