search

Home  >  Q&A  >  body text

objective-c - NSArray 对象里存储的元素为什么可以通过“数组下标”进行访问?

NSArray 不是OC里的内置数据类型,只是Cocoa库中的一个常用类。 那么用objectAtIndex方法,通过传入index参数进行访问NSArray里的元素,例如[arr objectAtIndex:5]即调用方法,这是可以理解的。但是arr[5]这种写法,为什么是属于合法的呢?按理说NSArray不是语言层的东西,应该是更高层级的,怎么还能有特殊的方法调用?

ringa_leeringa_lee2780 days ago926

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-04-21 11:19:18

    This is a new feature of Objective-c LLVM 4.0.

    Recommended reference article: http://blog.csdn.net/kindazrael/article/details/8091201

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-21 11:19:18

    The so-called syntactic sugar makes it more convenient for programmers to write. Almost all mainstream programming languages ​​support such features. It is not a question of whether it is advanced or not. Shouldn’t the more advanced it be, the more trouble it will have? There is no such truth.

    Going a step further, is there any difference between objectAtIndex and syntactic sugar? In the end, what the computer sees is 0 and 1, so what else is not syntactic sugar?

    reply
    0
  • Cancelreply