字典对象NSDictionary
NSDictionary用于描述字典对象,数组的下标是整型数,字典的下标是字符串类型的key
创建:
+ (instancetype)dictionary+ (instancetype)dictionaryWithObject:(ObjectType)anObject forKey:(id)aKey+ (instancetype)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray > *)keys+ (instancetype)dictionaryWithObjectsAndKeys:(id)firstObject, ...
字面值:
NSDictionary * dict = @{@"key1":@"value1", @"key2":@"value2”}; //常量方式
基本操作:
@property(readonly) NSUInteger count- (ObjectType)objectForKey:(KeyType)aKey
文件/URL相关:
+ (NSDictionary*)dictionaryWithContentsOfFile:(NSString *)path+ (NSDictionary *)dictionaryWithContentsOfURL:(NSURL *)aURL- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag- (BOOL)writeToURL:(NSURL *)aURL atomically:(BOOL)flag
可变字符串对象NSMutableDictionary
NSMutableDictionary用于描述可变字典,是NSDictionary的子类
- (void)setObject:(ObjectType)anObject forKey:(id)aKey- (void)removeObjectForKey:(KeyType)aKey- (void)removeAllObjects- (void)removeObjectsForKeys:(NSArray *)keyArray
plist文件
Property list文件是苹果Mac及iOS系统下使用的属性配置文件,是XML格式的文件。
Plist文件可以存放数值、数组、词典等。
Plist文件通常用于存储用户设置,也可用于存储捆绑的信息。
从plist文件中读取内容,使用NSArray、NSDictionary的contentOfFile方法
向plist文件中保存数据,使用NSArray、NSDictionary的writeToFile方法