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;