什么是ObjectiveC使用指针的最大优势 [英] What is the biggest advantage of using pointers in ObjectiveC

查看:139
本文介绍了什么是ObjectiveC使用指针的最大优势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认识到你的99%的人认为什么的H *** ......但是请帮我让我的头周围使用指针的这个概念。我敢肯定,我的具体问题,将有助于大量的新手。

I realize 99% of you think "what the h***…" But please help me to get my head around the this concept of using pointers. I'm sure my specific question would help lots of newbies.

我明白指针是,他们是在内存中ADRESS,并通过使用(*)运算符,你可以得到在该地址的值的引用。

I understand what pointers ARE and that they are a reference to an adress in memory and that by using the (*) operator you can get the value in that address.

让我们说:

int counter = 10;
int *somePointer = &counter;

现在我在计数的内存地址,我可以间接地指向它的价值做这个:

Now I have the address in memory of counter, and I can indirectly point to its value by doing this:

int x = *somePointer;

这使得 X = 10 ,对吧?

但是,这是最基本的例子,这种情况下,我可以使用 INT X =计数器; 和获取价值,所以请解释为什么指针真的是这样的在Objective-C和其他一些语言......重要的事情是什么情况下只会指针有意义吗?

But this is the most basic example, and for this case I could use int x = counter; and get that value, so please explain why pointers really are such an important thing in Objective-C and some other languages... in what case would only a pointer make sense?

鸭preciate它。

Appreciate it.

推荐答案

目标-C具有指针,因为它是C,它使用的指针广泛的演变。在这样的Objective-C的面向对象语言指针的好处是,你创建一个对象后,就可以围绕一个指针传递给对象,而不是绕过对象本身。换句话说,如果有一些对象,占用大量的存储空间,绕过一个指针是一个很大的存储器效率比绕过物体本身的副本。这可能不是简单的情况下,出现明显的,当你只处理基本类型,如 INT S,但是当你开始处理更复杂的对象内存和时间的节省是巨大的

Objective-C has pointers because it is an evolution of C, which used pointers extensively. The advantage of a pointer in an object-oriented language like Objective-C is that after you create an object, you can pass around a pointer to the object instead of passing around the object itself. In other words, if you have some object that takes up a large amount of storage space, passing around a pointer is a lot more memory-efficient than passing around a copy of the object itself. This may not be noticeable in simple cases when you’re only dealing with primitive types like ints, but when you start dealing with more complex objects the memory and time savings are enormous.

更重要的是,指针使它的的更轻松code的不同部分互相交谈。如果变量只能按价值而不是通过引用(这是当你使用指针会发生什么)被传递给函数,那么函数永远无法改变他们的投入。他们只可以通过任一返回值或改变这些通常会导致草率的,无组织的code一个全局变量,用眼过度改变你的程序的状态。

More importantly, pointers make it much easier for different parts of your code to talk to each other. If variables could only be passed to functions "by value" instead of "by reference" (which is what happens when you use pointers), then functions could never alter their inputs. They could only change the state of your program by either returning a value or by changing a global variable—the overuse of which generally leads to sloppy, unorganized code.

下面是一个具体的例子。假设你有一个Objective-C的方法,将JSON字符串解析并返回的NSDictionary

Here’s a concrete example. Suppose you have an Objective-C method that will parse a JSON string and return an NSDictionary:

+ (NSDictionary *)parseJsonString:(NSString *)json
                            error:(NSError **)error;

该方法会做解析,并返回一个的NSDictionary 如果一切正常。但是,如果有一些问题,输入字符串?我们希望的方式来提示用户(或至少程序员)发生了什么事,所以我们有一个指针指向一个 NSError ,其中将包含该信息。如果我们的方法失败(可能返回),我们可以取消引用错误参数,看看有什么地方出了错。我们已经有效地完成是给我们方法的返回值的两个不同的:通常情况下,它会返回一个的NSDictionary ,但它也可以返回一个 NSError

The method will do the parsing and return an NSDictionary if everything goes okay. But what if there’s some problem with the input string? We want a way to indicate to the user (or at least to the programmer) what happened, so we have a pointer to a pointer to an NSError, which will contain that information. If our method fails (probably returning nil), we can dereference the error parameter to see what went wrong. What we’ve effectively done is to give our method two different kinds of return values: usually, it will return an NSDictionary, but it could also return an NSError.

如果您想了解更多关于这一点,你可能有更好的运气搜索在C指针,而不是在Objective-C指针;指针在Objective-C中广泛使用的课程,但所有底层的机器是相同的C本身。

If you want to read more about this, you may have better luck searching for "pointers in C" rather than "pointers in Objective-C"; pointers are of course used extensively in Objective-C, but all of the underlying machinery is identical to that of C itself.

这篇关于什么是ObjectiveC使用指针的最大优势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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