OC中NSDictionary(字典)、NSMutableDictionary(可變字典)、NSSet(集合)、NSMutableSet(可變集合)得常用方法

字典用于保存具有映射關(guān)系數(shù)據(jù)的集合

一個(gè)key—value對(duì)認(rèn)為是一個(gè)條目(entry),字典是存儲(chǔ)key—value對(duì)的容器

與數(shù)組不同,字典靠key存取元素

key不能重復(fù),value必須是對(duì)象

鍵值對(duì)在字典中是無(wú)序存儲(chǔ)的

字典分:不可變字典(NSDictionary)和可變字典(NSMutableDictionary)

不可變字典一旦創(chuàng)建,鍵值對(duì)就不可更改,不可添加,不可刪除,僅能讀取key或者value

常用方法有:

1、創(chuàng)建字典對(duì)象

2、獲取所有key值,獲取所有value值

3、通過(guò)key值查詢value

1、常見(jiàn)字典的常用方法

在創(chuàng)建字典對(duì)象時(shí)需要賦值鍵值對(duì),但是順序?yàn)椋褐担I,(值在前鍵在后的形式)

NSDictionary *dic1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"Duke",@"name",@33,@"age",@"男",@"gender", nil];

NSLog(@"%@",dic1);

NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"Duke",@"name",@33,@"age",@"男",@"gender", nil];

NSLog(@"%@",dic2);

NSArray *keys = @[@"name",@"age",@"gender"];

NSArray *values = @[@"Duke",@33,@"male"];

字典的語(yǔ)法糖形式

鍵值對(duì)之間以逗號(hào)隔開,鍵和值之間以冒號(hào)隔開

NSDictionary *dic5 = @{@"name" : @"Duke",@"age" : @33,@"gender" : @"male"};

NSLog(@"%@",dic5);

創(chuàng)建字典對(duì)象時(shí)兩個(gè)數(shù)組元素個(gè)數(shù)必須一致

NSDictionary *dic3 = [[NSDictionary alloc] initWithObjects:values forKeys:keys];

NSLog(@"%@",dic3);

NSDictionary *dic4 = [NSDictionary dictionaryWithObjects:values forKeys:keys];

NSLog(@"%@",dic4);

通過(guò)count方法獲取字典中鍵值對(duì)的個(gè)數(shù)

NSInteger count = [dic4 count];

NSLog(@"%ld",count);

獲取字典中所有的鍵

NSArray *allKeys = [dic4 allKeys];

NSLog(@"%@",allKeys);

獲取字典中所有的值組成的值

NSArray *allValues = [dic4 allValues];

NSLog(@"%@",allValues);

通過(guò)指定的鍵獲取其在字典中對(duì)應(yīng)的值

id object = [dic4 objectForKey:@"age"];

NSLog(@"%@",object);

for (int i = 0; i < count; i++) {

id key = [allKeys objectAtIndex:i];

根據(jù)當(dāng)前遍歷得到的key去獲取對(duì)應(yīng)的value

id value = [dic4 objectForKey:key];

NSString *result = [value isKindOfClass:[NSString class]] ? @"YES" : @"NO";

NSLog(@"%@:%@-->%@",key,value,result);

}

NSMutableDictionary可變字典

可變NSMutableDictionary是NSDictionary的子類,擁有所有NSDictionary的方法

常用方法有:

1、創(chuàng)建字典對(duì)象

2、添加鍵值對(duì)

3、修改key對(duì)應(yīng)的value

4、刪除鍵值對(duì)

5、通過(guò)fou循環(huán)遍歷所有鍵值對(duì)

NSMutableDictionary可變字典對(duì)象的創(chuàng)建

NSMutableDictionary *dic6 = [[NSMutableDictionary alloc] initWithDictionary:dic5];

NSLog(@"%@",dic6);

NSMutableDictionary *dic7 = [NSMutableDictionary dictionaryWithDictionary:dic5];

NSLog(@"%@",dic7);

NSMutableDictionary *dic8 = [[NSMutableDictionary alloc] init];

NSLog(@"%@",dic8);

NSMutableDictionary *dic9 = [NSMutableDictionary dictionary];

NSLog(@"%@",dic9);

增加鍵值對(duì)

[dic9 setObject:@"Duke" forKey:@"name"];

[dic9 setObject:@"male" forKey:@"gender"];

[dic9 setObject:@33 forKey:@"age"];

NSLog(@"%@",dic9);

修改已有鍵對(duì)應(yīng)的值

如果鍵不存在,則為添加鍵值對(duì),如果鍵存在,則為修改已有鍵對(duì)應(yīng)的值

[dic9 setObject:@34 forKey:@"age"];

NSLog(@"%@",dic9);

根據(jù)指定鍵去刪除對(duì)應(yīng)的鍵值對(duì)

[dic9 removeObjectForKey:@"age"];

NSLog(@"%@",dic9);

刪除所有的鍵值對(duì)

[dic9 removeAllObjects];

NSLog(@"%@",dic9);

OC中的集合(NSSet)與數(shù)學(xué)中的集合一樣,集合中的元素具有唯一性、存儲(chǔ)單元的元素是無(wú)序的、存儲(chǔ)元素必須是對(duì)象類型

OC中用Set表示集合,分為NSSet和NSMutableSet

NSSet對(duì)象的創(chuàng)建 無(wú)序性,互異性

NSSet的常用方法

1、創(chuàng)建集合對(duì)象

2、獲取元素個(gè)數(shù)

3、獲取集合中的某個(gè)元素

4、判斷集合中是否包含某個(gè)對(duì)象

創(chuàng)建集合對(duì)象

NSSet *set1 = [[NSSet alloc] initWithObjects:@"1",@"2",@"2",@"3", nil];

NSLog(@"%@",set1);

NSSet *set2 = [NSSet setWithObjects:@"3",@"2",@"1", nil];

NSLog(@"%@",set2);

用數(shù)組對(duì)象來(lái)創(chuàng)建集合對(duì)象

可以通過(guò)這種方法過(guò)濾掉數(shù)組中重復(fù)的元素對(duì)象

NSArray *array = @[@1,@2,@3,@2,@3];

NSSet *set3 = [[NSSet alloc] initWithArray:array];

NSSet *set4 = [NSSet setWithArray:array];

NSLog(@"%@",set3);

NSLog(@"%@",set4);

獲取集合中對(duì)象的個(gè)數(shù)

NSInteger count2 = [set4 count];

NSLog(@"%ld",count2);

獲取集合中的對(duì)象

id object1 = [set4 anyObject];

NSLog(@"%@",object1);

判斷一個(gè)給定的對(duì)象是否包含在指定的集合中

NSString *result1 = [set4 containsObject:@3] ? @"YES" : @"NO";

NSLog(@"%@is contained in set %@",@3,result1);

NSMutableSet的常用方法

1、創(chuàng)建集合對(duì)象

2、添加元素

3、刪除元素

創(chuàng)建NSMutableset對(duì)象

NSMutableSet *mutableSet1 = [[NSMutableSet alloc] init];

NSLog(@"%@",mutableSet1);

NSMutableSet *mutableSet2 = [NSMutableSet set];

NSLog(@"%@",mutableSet2);

通過(guò)不可變對(duì)象創(chuàng)建

NSMutableSet *mutableSet3 = [[NSMutableSet alloc] initWithSet:set1];

NSLog(@"%@",mutableSet3);

NSMutableSet *mutableSet4 = [NSMutableSet setWithSet:set1];

NSLog(@"%@",mutableSet4);

添加集合元素

[mutableSet4 addObject:@4];

NSLog(@"%@",mutableSet4);

刪除單個(gè)集合

[mutableSet4 removeObject:@"3"];

NSLog(@"%@",mutableSet4);

刪除所有元素

[mutableSet4 removeAllObjects];

NSLog(@"%@",mutableSet4);

NSCountedSet

NSCountedSet是NSMutableString的子類

能記錄元素的重復(fù)次數(shù)

在set的基礎(chǔ)上添加了計(jì)數(shù)功能

NSCountedSet記錄添加進(jìn)去的集合對(duì)象出現(xiàn)的次數(shù)

NSCountedSet *countedSet = [NSCountedSet set];

[countedSet addObject:@1];

[countedSet addObject:@2];

[countedSet addObject:@2];

NSLog(@"%@",countedSet);

單獨(dú)獲取某個(gè)對(duì)象在集合中出現(xiàn)過(guò)多少次

NSInteger countOfObjec = [countedSet countForObject:@1];

NSLog(@"%ld",countOfObjec);

通過(guò)快速枚舉來(lái)遍歷數(shù)組元素

NSArray *testArray = @[@1,@2,@3,@4,@5];

for (id object in testArray) {

NSLog(@"%@",object);

}

for (NSNumber *object in testArray) {

NSLog(@"%@",object);

}

快速遍歷集合

for (id object in set1) {

NSLog(@"%@",object);

}

快速遍歷字典

直接遍歷字典直接得到的是字典的每一個(gè)鍵,可以通過(guò)遍歷得到的鍵去獲取對(duì)應(yīng)的值

for (NSString *key in dic1) {

NSLog(@"dictionary[%@]:%@",key,dic1[key]);

}

//??? dic1[key]就相當(dāng)于[dic1 objectForKey:key]


文章轉(zhuǎn)載自

OC中NSDictionary(字典)、NSMutableDictionary(可變字典)、NSSet(集合)、NSMutableSet(可變集合)得常用方法 - Running2Snail - 博客園

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容