在目标 c 中需要指针对象 [英] need of pointer objects in objective c

查看:65
本文介绍了在目标 c 中需要指针对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个非常基本的问题..但对于理解概念真的非常重要..在 c++ 或 c 语言中,我们通常不使用指针变量来存储值......即值简单地存储在:

A very basic question .. but really very important to understand the concepts.. in c++ or c languages, we usually don't use pointer variables to store values.. i.e. values are stored simply as is in:

int a=10;

但是在ios sdk中,在objective c中,我们使用的大多数对象都是通过用它们表示的指针来初始化的:

but here in ios sdk, in objective c, most of the objects which we use are initialized by denoting a pointer with them as in:

NSArray *myArray=[NSArray array];

所以,我想到了这个问题,使用指针对象有什么好处需要(这就是我们在这里所说的,如果它是不正确,请告诉).另外,在使用指针对象进行分配时,有时我会混淆内存分配基础知识.我可以在任何地方寻找好的解释吗?

So,the question arises in my mind ,that, what are the benefit and need of using pointer-objects (thats what we call them here, if it is not correct, please, do tell).. Also I just get confused sometimes with memory allocation fundamentals when using a pointer objects for allocation. Can I look for good explanations anywhere?

推荐答案

在c++或c语言中,我们通常不使用指针变量来存储值

in c++ or c languages, we usually don't use pointer variables to store values

我会去掉或 C"部分.C++ 程序员确实不赞成使用原始指针,但 C 程序员不反对.C 程序员喜欢指针,并将它们视为解决所有问题不可避免的灵丹妙药.(不,不是真的,但指针在 C 中仍然非常频繁地使用.)

I would take that "or C" part out. C++ programmers do frown upon the use of raw pointers, but C programmers don't. C programmers love pointers and regard them as an inevitable silver bullet solution to all problems. (No, not really, but pointers are still very frequently used in C.)

但是在ios sdk中,在objective c中,我们使用的大多数对象都是通过用它们表示一个指针来初始化的

but here in ios sdk, in objective c, most of the objects which we use are initialized by denoting a pointer with them

哦,仔细看:

大部分对象

更近:

对象

所以你说的是 Objective-C 对象,amirite?(忽略 C 标准本质上将所有值和变量描述为对象"的微妙之处.)

So you are talking about Objective-C objects, amirite? (Disregard the subtlety that the C standard essentially describes all values and variables as an "object".)

它实际上只是Objective-C 对象,它们在Objective-C 中始终是指针.由于 Objective-C 是 C 的严格超集,因此在编写 iOS 应用程序(或 OS X 应用程序,或任何其他基于 Objective-C 的程序)时,所有 C 习语和编程技术仍然适用.这是毫无意义的、多余的、浪费的,因此,写这样的东西甚至被认为是错误的

It's really just Objective-C objects that are always pointers in Objective-C. Since Objective-C is a strict superset of C, all of the C idioms and programming techniques still apply when writing iOS apps (or OS X apps, or any other Objective-C based program for that matter). It's pointless, superfluous, wasteful, and as such, it is even considered an error to write something like

int *i = malloc(sizeof(int));
for (*i = 0; *i < 10; ++*i)

仅仅因为我们在 Objective-C 领域.原语(或者更准确地说是 C++ 术语中的普通旧数据类型")仍然遵循如果不需要就不要使用指针"规则.

just because we are in Objective-C land. Primitives (or more correctly "plain old datatypes" with C++ terminology) still follow the "don't use a pointer if not needed" rule.

使用指针对象的好处和需要是什么

what are the benefit and need of using pointer-objects

那么,为什么它们是必要的:

Objective-C 是一种面向对象的动态语言.该语言的这两个密切相关的属性使程序员可以利用诸如 之类的技术多态性duck-typing动态绑定(是的,这些是超链接,点击它们).

Objective-C is an object-oriented and dynamic language. These two, strongly related properties of the language make it possible for programmers to take advantage of technologies such as polymorphism, duck-typing and dynamic binding (yes, these are hyperlinks, click them).

这些功能的实现方式使得所有对象都必须由指向它们的指针表示.让我们看一个例子.

The way these features are implemented make it necessary that all objects be represented by a pointer to them. Let's see an example.

编写移动应用程序时的一项常见任务是从服务器检索一些数据.现代基于 Web 的 API 使用 JSON 数据交换格式来序列化数据.这是一种简单的文本格式,可以解析(例如,使用 NSJSONSerialization 类)为各种类型的数据结构及其相应的集合类,例如 NSArrayNSDictionary.这意味着 JSON 解析器类/方法/函数必须返回一些通用的、可以表示数组和字典的东西.

A common task when writing a mobile application is retrieving some data from a server. Modern web-based APIs use the JSON data exchange format for serializing data. This is a simple textual format which can be parsed (for example, using the NSJSONSerialization class) into various types of data structures and their corresponding collection classes, such as an NSArray or an NSDictionary. This means that the JSON parser class/method/function has to return something generic, something that can represent both an array and a dictionary.

那么现在呢?我们不能返回一个非指针的 NSArrayNSDictionary 结构(Objective-C 对象实际上只是我知道 Objective-C 的所有平台上的普通旧 C 结构工作),因为它们的大小不同,它们具有不同的内存布局等.编译器无法理解代码.这就是为什么我们返回一个指针,指向一个通用的Objective-C对象,类型为id.

So now what? We can't return a non-pointer NSArray or NSDictionary struct (Objective-C objects are really just plain old C structs under the hoods on all platforms I know Objective-C works on), because they are of different size, they have different memory layouts, etc. The compiler couldn't make sense of the code. That's why we return a pointer to a generic Objective-C object, of type id.

C 标准要求指向 struct 的指针(因此指向对象)具有相同的表示和对齐要求 (C99 6.2.5.27),即.e.指向任何结构的指针都可以安全地转换为指向任何其他结构的指针.因此,这种方法是正确的,我们现在可以返回任何对象.使用运行时自省,还可以动态确定对象的确切类型(类),然后相应地使用它.

The C standard mandates that pointers to structs (and as such, to objects) have the same representation and alignment requirements (C99 6.2.5.27), i. e. that a pointer to any struct can be cast to a pointer to any other struct safely. Thus, this approach is correct, and we can now return any object. Using runtime introspection, it is also possible to determine the exact type (class) of the object dynamically and then use it accordingly.

为什么它们比非指针更方便或更好(在某些方面):

使用指针,不需要传递同一对象的多个副本.创建大量副本(例如,每次将对象分配给或传递给函数时)可能会很慢并导致性能问题 - 一个中等复杂的对象,例如视图或视图控制器,可能有几十个实例变量,因此单个实​​例可能测量数百个字节.如果一个接受对象类型的函数调用在一个紧密的循环中被调用数千或数百万次,那么重新分配和复制它是非常痛苦的(无论如何对于 CPU 来说),并且只传递一个更容易和更直接的指向对象的指针(尺寸更小,因此复制速度更快).此外,Objective-C 作为一种引用计数语言,无论如何都不鼓励"过度复制.保留和释放优于显式复制和释放.

Using pointers, there is no need to pass around multiple copies of the same object. Creating a lot of copies (for example, each time an object is assigned to or passed to a function) can be slow and lead to performance problems - a moderately complex object, for example, a view or a view controller, can have dozens of instance variables, thus a single instance may measure literally hundreds of bytes. If a function call that takes an object type is called thousands or millions of times in a tight loop, then re-assigning and copying it is quite painful (for the CPU anyway), and it's much easier and more straightforward to just pass in a pointer to the object (which is smaller in size and hence faster to copy over). Furthermore, Objective-C, being a reference counted language, even kind of "discourages" excessive copying anyway. Retaining and releasing is preferred over explicit copying and deallocation.

当使用指针对象进行分配时,我有时也会对内存分配基础感到困惑

Also I just get confused sometimes with memory allocation fundamentals when using a pointer objects for allocation

那么即使没有指针,您很可能也很困惑.不要把它归咎于指针,而是程序员的错误;-)

Then you are most probably confused enough even without pointers. Don't blame it on the pointers, it's rather a programmer error ;-)

这里是...

  • ...the official documentation and memory management guide by Apple;
  • ...the earliest related Stack Overflow question I could find;
  • ...something you should read before trying to continue Objective-C programming #1; (i. e. learn C first)
  • ...something you should read before trying to continue Objective-C programming #2;
  • ...something you should read before trying to continue Objective-C programming #3;
  • ...and an old Stack Overflow question regarding C memory management rules, techniques and idioms;

玩得开心!:-)

这篇关于在目标 c 中需要指针对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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