在Objective-C使用星号 [英] Asterisk usage in Objective-C

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

问题描述

我有关于使用在Objective-C星号的问题。只是要清楚:我知道指针是什么,一切都在程序C.我想,虽然两件事情:

1)为什么所有(引用)的Objective-C对象的指针?为什么不普通的变量? (即NSArray的阵列= [[NSArray的页头]初始化];)

2)调用方法时,你为什么忽略了星号?

谢谢!


解决方案

  

1)为什么所有(引用)
  Objective-C的对象的指针?为什么不
  纯变量? (即NSArray的阵列=
  [NSArray的页头]初始化];)


想想一个Objective-C的对象作为了一会儿一个荣耀的结构。

NSArray的阵列; 在局部范围将在堆栈中分配的对象。 的NSArray *阵列; 表示内存的大块支持的对象,通常从堆中分配

很久很久,以前它决定的Objective-C不支持在堆栈上的对象。大多数情况下,因为这样做会使得内存管理一个巨大的烂摊子(主要是, -retain 的堆栈上的东西没有任何意义的)。

由于Objective-C的设计为C的纯超集,不像C ++,不尝试修改C的基本行为,具有 * 在那里很自然。

注意 ID 打破这一点,但 ID 也是完全通用的类型。它是,事实上,可能有在堆栈上Objective-C的对象。在Snow Leopard中,块实际上是Objective-C的对象和他们在栈上启动。这是没有,不过,支持您在栈上创建自己的对象。


  

2)你为什么忽略了星号的时候
  调用的方法?


由于您没有取消引用指向对象的指针和指向的对象是方法的实现本身的关键。当你说...

  [anArray objectAtIndex:5];

...这是完全等同于书写...

  objc_msgSend(anArray,@selector(objectAtIndex :),5);

...当你实现所述方法....

   - (ID)objectAtIndex:(NSUInteger)anIndex处;

...这是完全等同于实现此C函数...

  ID object_at_index(ID自我,SEL _cmd,NSUInteger anIndex处){...}

这是一个Objective-C的方法其实只是一个C函数,需要两个或多个参数。作为事实上,你可以在此行code的粘贴到任何方法实现,它会只是工作:

 的NSLog(@方法%@,NSStringFromSelector(_cmd));

I had a question regarding the use of asterisks in Objective-C. Just to be clear: I understand what pointers are and everything in procedural C. I was wondering two things though:

1) Why are all (references to) Objective-C objects pointers? Why not plain variables? (i.e. NSArray array = [[NSArray alloc] init];)

2) Why do you omit the asterisk when calling method?

Thanks!

解决方案

1) Why are all (references to) Objective-C objects pointers? Why not plain variables? (i.e. NSArray array = [[NSArray alloc] init];)

Think of an Objective-C object as a glorified struct for a moment.

NSArray array; in a local scope would be an object "allocated" on the stack. NSArray *array; indicates an object backed by a hunk of memory, typically allocated from the heap.

Long, long, ago it was decided that Objective-C wouldn't support objects on the stack. Mostly, because doing so would make memory management a gigantic mess (mainly, -retain of something on the stack doesn't make any sense at all).

Since Objective-C was designed as a pure superset of C and, unlike C++, doesn't try to modify the basic behavior of C, having the * in there seemed natural.

Note that id breaks this, but id is also a completely generic type. And it is, in fact, possible to have Objective-C objects on the stack. In Snow Leopard, Blocks are actually Objective-C objects and they do start on the stack. It isn't, however, supported for you to create your own objects on the stack.

2) Why do you omit the asterisk when calling method?

Because you aren't dereferencing the pointer to the object and that pointer to the object is critical within the method implementation itself. When you say...

[anArray objectAtIndex: 5];

... it is exactly equivalent to writing ...

objc_msgSend(anArray, @selector(objectAtIndex:), 5);

... and when you implement said method ....

- (id) objectAtIndex: (NSUInteger) anIndex;

... it is exactly equivalent to implementing this C function ...

id object_at_index(id self, SEL _cmd, NSUInteger anIndex) { ... }

That is, an Objective-C method is really just a C function that takes two or more arguments. As a matter of fact, you can paste this line of code into any method implementation and it'll "just work":

NSLog(@"method %@", NSStringFromSelector(_cmd));

这篇关于在Objective-C使用星号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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