foundation——容器——创建 初始化

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

NSMutableDictionary

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值