NSArray and its subclass NSMutableArray manage ordered collections of objects called arrays.
NSArray creates static arrays, and NSMutableArray creates dynamic arrays.
You can use arrays when you need an ordered collection of objects.
NSArray 用于保存对象的有序集合,但只能保存 OC 对象(继承自 NSObject 的 interface)。由于 Objective-C++ 是动态定型(dynamically typed),继承自NSObject 的 interface type cannot be statically allocated。因此 NSArray 本质上保存的是id,即 NSObject* 泛型指针。最新版 SDK 头文件已将数组元素声明为支持
Cocoa 的 NSArray 是基于 C 底层 CFArray/CFArrayRef 实现的,NSArray 可以看做是一个 CFArrayRef 的 Wrapper类。
__NSArrayI(Immutable)是NSArray的真正类型(_internal),__NSArrayM(Mutable)是NSMutableArray的真正类型(_internal)。
[objc] view plain copy print?
@interface NSArray<__covariant ObjectType> : NSObject
@interface NSMutableArray
C 原生数据类型(Native Data Type: int,char,double,etc)不能直接作为 NSArray 元素,它们必须通过装箱(boxing)成 NSNumber、NSString 或 NSValue 等 OC 对象才能纳入 OC 数组存储。
在苹果 WWDC2012 大会上介绍了大量 Objective-C 的新特性,其中有一点就是Objective-C Literals(参考1、2、3),它允许你在XCode 4.4/LLVM Compiler 4.0/iOS 6及以上平台方便地基于字面量定义数字、数组和字典常量对象。
[plain] view plain copy print?
Three new features were introduced into clang at the same time:
- NSNumber Literals provide a syntax for creating NSNumber from scalar literal expressions;
- Collection Literals provide a short-hand for creating arrays and dictionaries;
- Object Subscripting provides a way to use subscripting with Objective-C objects.
Users of Apple compiler releases can use these features starting with the Apple LLVM Compiler 4.0.
Users of open-source LLVM.org compiler releases can use these features starting with clang v3.1.
APPLE LLVM CLANG 前端(参考LLVM 之 Clang、LLVM 与 Clang)支持字面语法,它使用简化符号 @ 声明编译器指令来定义对象,类似 java5 提供的 auto boxing 功能。这虽然是一个语法糖,但对提高写代码效率帮助很大。