天天看點

Foundation架構(4)字典對象及plist檔案

  • 字典對象NSDictionary

NSDictionary用于描述字典對象,數組的下标是整型數,字典的下标是字元串類型的key

建立:

+ (instancetype)dictionary
+ (instancetype)dictionaryWithObject:(ObjectType)anObject forKey:(id<NSCopying>)aKey
+ (instancetype)dictionaryWithObjects:(NSArray<ObjectType> *)objects forKeys:(NSArray<id<NSCopying>> *)keys
+ (instancetype)dictionaryWithObjectsAndKeys:(id)firstObject, ...      

字面值:

NSDictionary * dict = @{@"key1":@"value1", @"key2":@"value2”};  //常量方式      

基本操作:

@property(readonly) NSUInteger count
- (ObjectType)objectForKey:(KeyType)aKey      

檔案/URL相關:

+ (NSDictionary<KeyType,ObjectType> *)dictionaryWithContentsOfFile:(NSString *)path
+ (NSDictionary<KeyType,ObjectType> *)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<NSCopying>)aKey
- (void)removeObjectForKey:(KeyType)aKey
- (void)removeAllObjects
- (void)removeObjectsForKeys:(NSArray<KeyType> *)keyArray      
  • plist檔案

Property list檔案是蘋果Mac及iOS系統下使用的屬性配置檔案,是XML格式的檔案。

     Plist檔案可以存放數值、數組、詞典等。

     Plist檔案通常用于存儲使用者設定,也可用于存儲捆綁的資訊。

Foundation架構(4)字典對象及plist檔案