標籤:
1.不可變資料字典(NSDicionary)
字典:用來儲存具有一一對應關係的資料.
一個key 對應一個 value ,key起到了唯一標示的作用,key必須是唯一的,但是一個vlaue可以對應多個key.
字典儲存的是無序的元素,一對鍵值對組成了字典中的一個元素.
①.不可變字典初始化賦值
<span style="font-size:18px;"> //笑笑文法 NSDictionary *dic = @{@"name":@"zhangdan",@"age":18,@"gender":@"man"}; //初始化 NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys: @"放棄",@"abandon",@"對象",@"object",@"索引",@"index", nil]; NSLog(@"%@",dictionary); //遍曆構造器方法 NSDictionary *dictionary1 = [NSDictionary dictionaryWithObjectsAndKeys: @"xiaozang",@"name",@"18",@"age",@"23342432",@"phone",nil]; NSLog(@"%@",dictionary1);</span>②.求字典元素個數,即擷取所有的key和value
<span style="font-size:18px;"> //擷取元素的個數 NSUInteger num = [dictionary1 count]; NSLog(@"%ld",num); //擷取所有的key for (NSString *temp in [dictionary1 allKeys] ) { NSLog(@"%@",temp); } NSArray *array = [dictionary1 allKeys]; NSLog(@"%@",array); //根據key擷取對應的value NSString *value = [dictionary1 objectForKey:@"age"];</span>③.遍曆字典:取得key,和value
<span style="font-size:18px;"> //字典遍曆:取得的是key,然後根據key再擷取對應的value for (NSString *temp in [dictionary1 allKeys] ) { NSString *value = [dictionary1 objectForKey:temp]; NSLog(@"%@ = %@",temp,value); }</span>④.其他一些操作
<span style="font-size:18px;">//1.判斷是否和另一個字典相同- (BOOL)isEqualToDictionary:(NSDictionary *)otherDictionary;//2.同過字典中所有的vlaue給所有的keys排序,用到block文法.- (NSArray *)keysSortedByValueUsingComparator:(NSComparator)cmptr</span>
2.可變字典(NSMutableDictionary)
可變字典繼承不可變字典,繼承不可變字典內的方法,同時增加了刪除,添加,修改操作.
①.建立字典對象,初始化賦值操作
<span style="font-size:18px;"> //笑笑文法 NSMutaleDicrtionary *mut = [@{@"name":@"zhangdan",@"age":12} mutableCopy]; //初始化 NSMutableDictionary *mutable = [[NSMutableDictionary alloc] init]; //便利構造器方法 NSMutableDictionary *mutable = [NSMutableDictionary dictionaryWithCapacity:1]; //便利構造器建立對象方法 NSMutableDictionary *mutable1 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"21",@"age",@"asdf",@"name",nil]; NSLog(@"%@",mutable1);</span><p><span style="font-size:18px;"></span></p><span style="font-size:18px;"></span>
②向可變資料字典中,添加,刪除,修改元素
<span style="font-size:18px;"> //往字典中添加元素. [mutable setObject:@"13" forKey:@"age"]; NSLog(@"%@",mutable); //根據key刪除對應的元素 [mutable removeObjectForKey:@"age"]; NSLog(@"%@",mutable); //修改key對應的value [mutable setObject:@"1sad8" forKey:@"age"]; NSLog(@"%@",mutable); [mutable setValue:@"17" forKey:@"age"]; //移除所有的元素 [mutable removeAllObjects]; //將字典中原來的元素替換為另一個字典中的元素 [mutable setDictionary:otherDictionary]; //快速根據key取出對應的value; NSString *s1 = dictionary[@"name"];//[dictionary objectForKey:@"name"] NSLog(@"%@",s1); NSString *s2 = dictionary[@"age"]; NSLog(@"%@",s2);</span>
3,集合(NSSet和NSMutableSet)
1).集合:無序,不能添加重複元素
2).可變集合是不可變集合的子類,包含父類的所有方法,同時可變集合增加了刪除,修改,添加操作.
①不可變集合常見操作.
<span style="font-size:18px;"> //初始化方法 NSSet *set = [[NSSet alloc] initWithObjects:@"aa",@"dd",nil]; //便利構造器方法 NSSet *set = [[NSSet alloc] initWithObjects:@"aa",@"dd",nil];<span style="font-size:18px;"> //計算元素個數 NSUInteger num = [set count]; NSLog(@"%ld",num); NSLog(@"%@",set); //擷取集合中的元素 NSLog(@"%@",[set anyObject]); //快速遍曆 for (NSString *temp in set) { NSLog(@"%@",temp); } //判斷集合是否包含某對象 BOOL isContain = [set containsObject :@"dd"]; //判斷兩個集合是否相同 - (BOOL)isEqualToSet:(NSSet *)otherSet; //判斷集合中的子串是否和傳入的相同, - (BOOL)isSubsetOfSet:(NSSet *)otherSet;</span></span>
②.可變集合操作
<span style="font-size:18px;"> //建立對象 NSMutableSet *set = [NSMutableSet setWithObjects:@"aa",@"bb",@"cc",@"dd",nil]; NSMutableSet *set1 = [NSMutableSet setWithCapacity:1];<span style="font-size:18px;"> //添加 [set addObject:@"rrr"]; NSLog(@"%@",set); //刪除 [set removeObject:@"aa"]; NSLog(@"%@",set); //刪除所有 [set removeAllObjects]; NSLog(@"%@",set); //添加集合中的元素,通過數組 [set addObjectsFromArray:array]; //將集合中的元素替換成另一個集合的元素 [set setSet:otherSet];4.NSCountedSet</span></span><p><span style="font-size:18px;"> NSCountedSet的父類是NSMutableSet,所以繼承了NSset和NSMutableSet裡面的方法.</span></p>
常見操作
<span style="font-size:18px;"> NSCountedSet *countSet = [NSCountedSet setWithObjects: @"aa",@"bb",@"cc",@"dd",@"ee",@"aa",nil]; //數組中的重複個數 NSCountedSet *count = [[NSCountedSet alloc] initWithCapacity:1]; <span style="font-size:18px;"> NSUInteger count = [countSet countForObject:@"aa"]; NSLog(@"%ld",count);</span></span>
//添加元素
- (void)addObject:(id)object;
//移出對象
- (void)removeObject:(id)object;
例題:有100個數,每個數取值範圍為20 - 55 ,找出其中重複的數,重複次數.
<span style="font-size:18px;"> NSCountedSet *countSet = [NSCountedSet setWithCapacity:100]; for (int i = 0;i < 100; i++) { int random = arc4random() % (55 - 22 + 1) + 20; printf("%d ",random); [countSet addObject:@(random)]; } for (NSNumber *num in countSet) { NSUInteger count = [countSet countForObject:num];//記錄重複的次數. if (count > 1) { NSLog(@"%d,重複次數為%ld",[num intValue],count); } }</span>
總結:字典,集合.數組.的相同點和不同點
<span style="font-size:18px;"> /** * * 相同點:都屬於collection(集合),用來儲存多個對象,並不限制對象的類型 * 不同點: * 1.作用: * 數組管理有序的集合 * 字典管理具有一一對應關係的資料集合 * 集合管理無序並且互異性的元素的集合. * 2.特點 * 數組:有序,並且元素可以重複. * 字典:無序,每一個元素都是一對索引值對,一個key只能對應一個value,但是一個value可以對應多個key,key是唯一的. * 集合:無序,互異性(元素不能重複,但會記錄重複次數.) * 3.取值方式: * 數組:通過下標擷取對應元素. * 字典:通過key擷取對用的value. * 集合:隨機取值.anyObject,集合的效率最大化. * 4.快速遍曆:for in遍曆時不能修改遍曆的集合 * 數組:遍曆出來的時數組中的元素 * 字典:遍曆出來的是字典中的key,通過key,取出對應的value值. * 集合:遍曆出集合中的元素. * 5.建立對象的方法 1.初始化方法 2.便利構造器 * 數組 : initWithObjects: arrayWithObjects: * 字典 : initWithObjectsAndKeys: dictionaryWithObjectsAndKeys: * 集合 : initWithObjects: setWithObjects: */</span>
【學習ios之路:Object-C】字典.集合.