NSArray
create
+ (instancetype)array;
+ (instancetype)arrayWithArray:(NSArray<objecttype> *)array;
+ (nullable NSArray<objecttype> *)arrayWithContentsOfFile:(NSString *)path;
+ (nullable NSArray<objecttype> *)arrayWithContentsOfURL:(NSURL *)url;
+ (instancetype)arrayWithObject:(ObjectType)anObject;
+ (instancetype)arrayWithObjects:(ObjectType)firstObj, ... NS_REQUIRES_NIL_TERMINATION;
+ (instancetype)arrayWithObjects:(const ObjectType [])objects count:(NSUInteger)cnt;
init
- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithArray:(NSArray<objecttype> *)array;
- (instancetype)initWithArray:(NSArray<objecttype> *)array copyItems:(BOOL)flag;
- (nullable NSArray<objecttype> *)initWithContentsOfFile:(NSString *)path;
- (nullable NSArray<objecttype> *)initWithContentsOfURL:(NSURL *)url;
- (instancetype)initWithObjects:(ObjectType)firstObj, ... NS_REQUIRES_NIL_TERMINATION;
- (instancetype)initWithObjects:(const ObjectType [])objects count:(NSUInteger)cnt NS_DESIGNATED_INITIALIZER;
总结
注意:
- arrayWithContentsOfFile和arrayWithContentsOfURL对应的内容包含的只能是属性列表对象,解析出来的放入array的都是immutable对象
- arrayWithObject和arrayWithObjects传入的必须是non-nil对象(NSArray会进行nil判断,只有非nil才会加入NSArray),arrayWithObjects必须以nil结束,nil并不会被放入数组,只是起哨兵作用(可变参数结束标志)
注意:
- initWithArray:copyItems,当flag = YES时,取array元素copy,此时array元素必须遵守NSCopying协议,当flag = NO时,跟initWithArray一样,只是retain
NSMutableArray
NSSet
create
+ (instancetype)set;
+ (instancetype)setWithArray:(NSArray<objecttype> *)array;
+ (instancetype)setWithObject:(ObjectType)object;
+ (instancetype)setWithObjects:(ObjectType)firstObj, ... NS_REQUIRES_NIL_TERMINATION;
+ (instancetype)setWithObjects:(const ObjectType [])objects count:(NSUInteger)cnt;
+ (instancetype)setWithSet:(NSSet<objecttype> *)set;
注意:
- setWithObject和setWithObjects传入的必须是non-nil对象,setWithObjects必须以nil结束,nil并不会被放入数组,只是起哨兵作用(可变参数结束标志)
init
- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithArray:(NSArray<ObjectType> *)array;
- (instancetype)initWithObjects:(ObjectType)firstObj, ... NS_REQUIRES_NIL_TERMINATION;
- (instancetype)initWithObjects:(const ObjectType [])objects count:(NSUInteger)cnt NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithSet:(NSSet<objecttype> *)set;
- (instancetype)initWithSet:(NSSet<objecttype> *)set copyItems:(BOOL)flag;
注意:
- init基本上有对应的create版本,功能类似,create直接返回set实例对象,init需要先调用alloc(allocWithZone)生成实例对象,然后init
- initWithSet:copyItems,当flag=YES时,取set元素copy,此时set元素必须遵守NSCopying协议,当flag=NO时,跟initWithSet一样,只是retain
NSMutableSet
NSCountedSet
NSDictionary
create
+ (instancetype)dictionary;
+ (nullable NSDictionary<keytype, objecttype> *)dictionaryWithContentsOfFile:(NSString *)path;
+ (nullable NSDictionary<keytype, objecttype> *)dictionaryWithContentsOfURL:(NSURL *)url;
+ (instancetype)dictionaryWithDictionary:(NSDictionary<keytype, objecttype> *)dict;
+ (instancetype)dictionaryWithObject:(ObjectType)object forKey:(KeyType <nscopying>)key;
+ (instancetype)dictionaryWithObjects:(NSArray<objecttype> *)objects forKeys:(NSArray<KeyType <nscopying>> *)keys;
#if TARGET_OS_WIN32
+ (instancetype)dictionaryWithObjects:(const ObjectType [])objects forKeys:(const KeyType [])keys count:(NSUInteger)cnt;
#else
+ (instancetype)dictionaryWithObjects:(const ObjectType [])objects forKeys:(const KeyType <nscopying> [])keys count:(NSUInteger)cnt;
#endif
+ (instancetype)dictionaryWithObjectsAndKeys:(id)firstObject, ... NS_REQUIRES_NIL_TERMINATION NS_SWIFT_UNAVAILABLE("Use dictionary literals instead");
注意:
- dictionaryWithContentsOfFile和dictionaryWithContentsOfURL对应的内容包含的只能是属性列表对象(根对象是dictionary),解析出来的放入dictionary的都是immutable对象
- dictionary的key和object都必须是nil对象,放入dictionary的key对象都是copy,因此key对象必须遵守NSCopying协议,dictionaryWithObjectsAndKeys必须以nil结束,nil并不会被放入数组,只是起哨兵作用(可变参数结束标志)
- keys和objects数量必须一致,否则exception
init
- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (nullable NSDictionary<keytype, objecttype> *)initWithContentsOfFile:(NSString *)path;
- (nullable NSDictionary<keytype, objecttype> *)initWithContentsOfURL:(NSURL *)url;
- (instancetype)initWithDictionary:(NSDictionary<keytype, objecttype> *)otherDictionary;
- (instancetype)initWithDictionary:(NSDictionary<keytype, objecttype> *)otherDictionary copyItems:(BOOL)flag;
- (instancetype)initWithObjects:(NSArray<objecttype> *)objects forKeys:(NSArray<KeyType <nscopying>> *)keys;
#if TARGET_OS_WIN32
- (instancetype)initWithObjects:(const ObjectType [])objects forKeys:(const KeyType [])keys count:(NSUInteger)cnt;
#else
- (instancetype)initWithObjects:(const ObjectType [])objects forKeys:(const KeyType <nscopying> [])keys count:(NSUInteger)cnt NS_DESIGNATED_INITIALIZER;
#endif
- (instancetype)initWithObjectsAndKeys:(id)firstObject, ... NS_REQUIRES_NIL_TERMINATION;
注意:
- init基本上有对应的create版本,功能类似,create直接返回dictionary实例对象,init需要先调用alloc(allocWithZone)生成实例对象,然后init
- initWithDictionary:copyItems,当flag=YES时,取object的copy,此时object必须遵守NSCopying协议,当flag=NO时,跟initWithDictionary一样,只是retain