数组,字典基础整理

NSArray

数组创建

- (id)initWithObjects:(id)firstObj, ... , nil;
- (id)initWithArray:(NSArray *)array copyItems:(BOOL)flag; 
+ (id)arrayWithObjects:(id)firstObj, ... , nil;
- (id)initWithArray:(NSArray *)array;
+ (id)arrayWithArray:(NSArray *)array;
- (id)initWithContentsOfFile:(NSString *)path; 
+ (id)arrayWithContentsOfFile:(NSString *)path;

获取数组元素

- (id)objectAtIndex:(NSUInteger)index;
- (id)lastObject; 
- (id)firstObject;

计算数组长度

- (NSUInteger)count; // 只能用在一维数组中

合并字符串数组为字符串

- (NSString *)componentsJoinedByString:(NSString *)separator;// Each element in the array must handle description.

描述

- (NSString *)description; 

数组枚举器;枚举器遍历数组

- (NSEnumerator *)objectEnumerator; // 正序遍历时 不能修改数组
- (NSEnumerator *)reverseObjectEnumerator;

归档;存入plist文件中

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile; 
// 当bool值为YES时先将文件存入临时文件,没错误后再改名为path指定的文件。当为no时,直接写path指定的文件

NSMutableArray

创建可变数组

+ (id)array;
+ (id)arrayWithCapacity:(NSUInteger)numItems;
- (id)initWithCapacity:(NSUInteger)numItems;

增加数组元素

- (void)addObject:(id)anObject;
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
- (void)addObjectsFromArray:(NSArray *)otherArray;

修改数组元素

- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray; 
- (void)setArray:(NSArray *)otherArray;

删除数组元素

- (void)removeAllObjects;
- (void)removeLastObject;
- (void)removeObjectAtIndex:(NSUInteger)index;
- (void)removeObjectsInArray:(NSArray *)otherArray;
- (void)removeObjectsInRange:(NSRange)range;
- (void)removeObjectsAtIndexes:(NSIndexSet *)indexes;

NSDictonary

字典创建

// 对象方法创建
- (id)initWithObjectsAndKeys:(id)firstObject, ... , nil;
// 浅拷贝,拷贝时只拷贝字典内元素的指针(对字典元素进行浅拷贝)
- (id)initWithDictionary:(NSDictionary *)otherDictionary;
// 深拷贝,对字典内元素使用深拷贝
- (id)initWithDictionary:(NSDictionary *)otherDictionary copyItems:(BOOL)flag; 
// 使用数组来进行字典的初始化,键值数组个数必须相同,键必须能调用copy方法。值应该是浅拷贝。
- (id)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys; 
- (id)initWithContentsOfFile:(NSString *)path;

类方法创建

+ (id)dictionaryWithObjectsAndKeys:(id)firstObject, ... , nil;
+ (id)dictionaryWithDictionary:(NSDictionary *)dict; // 浅拷贝,深拷贝必须用对象方法
+ (id)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;
+ (id)dictionaryWithContentsOfFile:(NSString *)path;

查看键所对应的值

- (id)objectForKey:(id)aKey;
- (NSArray *)objectsForKeys:(NSArray *)keys notFoundMarker:(id)marker;

字典枚举器遍历

- (NSUInteger *)count;

获取所有键和值;字典遍历

- (NSArray *)allKeys;
- (NSArray *)allValues

字典枚举器遍历遍历

- (NSEnumerator *)keyEnumerator;

在字典中寻找某个值

- (NSArray *)allKeysForObject:(id)anObject;

归档;存入plist文件中

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile; 
// 当bool值为YES时先将文件存入临时文件,没错误后再改名为path指定的文件。当为no时,直接写path指定的文件

NSMutableDictionay

字典创建

- (id)initWithCapacity:(NSUInteger)numItems;
+ (id)dictionary;
+ (id)dictionaryWithCapacity:(NSUInteger)numItems;

向字典中增加元素

- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary; 
- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey; 
// 字典对所有key会进行copy操作,字典所有键必须能够copy

修改字典元素

- (void)setDictionary:(NSDictionary *)otherDictionary; // remove
- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey; // 对原先字典进行removeAllObjects ,

删除字典元素

- (void)removeObjectForKey:(id)aKey;
- (void)removeAllObjects;
- (void)removeObjectsForKeys:(NSArray *)keyArray;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值