【前言】
1,字典是一種以鍵值對(duì)的形式來存儲(chǔ)數(shù)據(jù)的數(shù)據(jù)結(jié)構(gòu)
2,字典中得鍵和值都可以是任意類型的對(duì)象审葬,但是鍵一般是字符串
3,鍵是手段奕谭,值是目的
4,字典中得元素沒有順序
5,字典中得鍵是唯一的血柳,值可以重復(fù)
-
NSDictionary
// 創(chuàng)建方法
// 實(shí)例化方法
- (id)initWithObjectsAndKeys:(id)firstObject, ...;
- (id)initWithDictionary:(NSDictionary *)otherDictionary;
// 類方法創(chuàng)建
+ (id)dictionaryWithObjectsAndKeys:(id)firstObject, ...;
+ (id)dictionaryWithDictionary:(NSDictionary *)dict;
// 獲取鍵值對(duì)個(gè)數(shù)
- (NSUInteger)count;
// 根據(jù)key 獲取相應(yīng)的值
- (id)objectForKey:(id)aKey;
// 獲取字典中所有的key
- (NSArray *)allKeys;
// 獲取值是anObject的所有的key
- (NSArray *)allKeysForObject:(id)anObject;
// 獲取字典中所有的值
- (NSArray *)allValues;
-
NSMutableDictionary
<1>增加
//增加整個(gè)字典
- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;
//增加鍵值對(duì)
//key不存在表示增加 key存在表示修改key對(duì)應(yīng)的值
- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey;
<3>刪除
//根據(jù)key刪除鍵值對(duì)
- (void)removeObjectForKey:(id)aKey;
//刪除所有鍵值對(duì)
- (void)removeAllObjects;
//根據(jù)數(shù)組中的內(nèi)容刪除相應(yīng)的鍵值對(duì)
- (void)removeObjectsForKeys:(NSArray *)keyArray;
<4>修改
//修改整個(gè)字典
- (void)setDictionary:(NSDictionary *)otherDictionary;