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


字典用於保存具有映射關系數據的集合
一個key—value對認為是一個條目(entry),字典是存儲key—value對的容器
與數組不同,字典靠key存取元素
key不能重復,value必須是對象
鍵值對在字典中是無序存儲的
字典分:不可變字典(NSDictionary)和可變字典(NSMutableDictionary)
不可變字典一旦創建,鍵值對就不可更改,不可添加,不可刪除,僅能讀取key或者value
常用方法有:
1、創建字典對象
2、獲取所有key值,獲取所有value值
3、通過key值查詢value
1、常見字典的常用方法
在創建字典對象時需要賦值鍵值對,但是順序為:值,鍵,(值在前鍵在后的形式)
    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"];
字典的語法糖形式
鍵值對之間以逗號隔開,鍵和值之間以冒號隔開
    NSDictionary *dic5 = @{@"name" : @"Duke",@"age" : @33,@"gender" : @"male"};
    NSLog(@"%@",dic5);
創建字典對象時兩個數組元素個數必須一致
    NSDictionary *dic3 = [[NSDictionary alloc] initWithObjects:values forKeys:keys];
    NSLog(@"%@",dic3);
    NSDictionary *dic4 = [NSDictionary dictionaryWithObjects:values forKeys:keys];
    NSLog(@"%@",dic4);
通過count方法獲取字典中鍵值對的個數
    NSInteger count = [dic4 count];
    NSLog(@"%ld",count);
獲取字典中所有的鍵
    NSArray *allKeys = [dic4 allKeys];
    NSLog(@"%@",allKeys);
獲取字典中所有的值組成的值
    NSArray *allValues = [dic4 allValues];
    NSLog(@"%@",allValues);
通過指定的鍵獲取其在字典中對應的值
    id object = [dic4 objectForKey:@"age"];
    NSLog(@"%@",object);

    for (int i = 0; i < count; i++) {
        id key = [allKeys objectAtIndex:i];
根據當前遍歷得到的key去獲取對應的value
        id value = [dic4 objectForKey:key];
        NSString *result = [value isKindOfClass:[NSString class]] ? @"YES" : @"NO";
        NSLog(@"%@:%@-->%@",key,value,result);
    }
NSMutableDictionary可變字典
可變NSMutableDictionary是NSDictionary的子類,擁有所有NSDictionary的方法
常用方法有:
1、創建字典對象
2、添加鍵值對
3、修改key對應的value
4、刪除鍵值對
5、通過fou循環遍歷所有鍵值對
NSMutableDictionary可變字典對象的創建
    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);
增加鍵值對
    [dic9 setObject:@"Duke" forKey:@"name"];
    [dic9 setObject:@"male" forKey:@"gender"];
    [dic9 setObject:@33 forKey:@"age"];
    NSLog(@"%@",dic9);
修改已有鍵對應的值
如果鍵不存在,則為添加鍵值對,如果鍵存在,則為修改已有鍵對應的值
    [dic9 setObject:@34 forKey:@"age"];
    NSLog(@"%@",dic9);
根據指定鍵去刪除對應的鍵值對
    [dic9 removeObjectForKey:@"age"];
    NSLog(@"%@",dic9);
刪除所有的鍵值對
    [dic9 removeAllObjects];
    NSLog(@"%@",dic9);
OC中的集合(NSSet)與數學中的集合一樣,集合中的元素具有唯一性、存儲單元的元素是無序的、存儲元素必須是對象類型
OC中用Set表示集合,分為NSSet和NSMutableSet
NSSet對象的創建 無序性,互異性
NSSet的常用方法
1、創建集合對象
2、獲取元素個數
3、獲取集合中的某個元素
4、判斷集合中是否包含某個對象
創建集合對象
    NSSet *set1 = [[NSSet alloc] initWithObjects:@"1",@"2",@"2",@"3", nil];
    NSLog(@"%@",set1);
    NSSet *set2 = [NSSet setWithObjects:@"3",@"2",@"1", nil];
    NSLog(@"%@",set2);
用數組對象來創建集合對象
可以通過這種方法過濾掉數組中重復的元素對象
    NSArray *array = @[@1,@2,@3,@2,@3];
    NSSet *set3 = [[NSSet alloc] initWithArray:array];
    NSSet *set4 = [NSSet setWithArray:array];
    NSLog(@"%@",set3);
    NSLog(@"%@",set4);
獲取集合中對象的個數
    NSInteger count2 = [set4 count];
    NSLog(@"%ld",count2);
獲取集合中的對象
    id object1 = [set4 anyObject];
    NSLog(@"%@",object1);
判斷一個給定的對象是否包含在指定的集合中
    NSString *result1 = [set4 containsObject:@3] ? @"YES" : @"NO";
    NSLog(@"%@is contained in set %@",@3,result1);
NSMutableSet的常用方法
1、創建集合對象
2、添加元素
3、刪除元素
創建NSMutableset對象
    NSMutableSet *mutableSet1 = [[NSMutableSet alloc] init];
    NSLog(@"%@",mutableSet1);
    NSMutableSet *mutableSet2 = [NSMutableSet set];
    NSLog(@"%@",mutableSet2);
通過不可變對象創建
    NSMutableSet *mutableSet3 = [[NSMutableSet alloc] initWithSet:set1];
    NSLog(@"%@",mutableSet3);
    NSMutableSet *mutableSet4 = [NSMutableSet setWithSet:set1];
    NSLog(@"%@",mutableSet4);
添加集合元素
    [mutableSet4 addObject:@4];
    NSLog(@"%@",mutableSet4);
刪除單個集合
    [mutableSet4 removeObject:@"3"];
    NSLog(@"%@",mutableSet4);
刪除所有元素
    [mutableSet4 removeAllObjects];
    NSLog(@"%@",mutableSet4);
NSCountedSet
NSCountedSet是NSMutableString的子類
能記錄元素的重復次數
在set的基礎上添加了計數功能
NSCountedSet記錄添加進去的集合對象出現的次數
    NSCountedSet *countedSet = [NSCountedSet set];
    [countedSet addObject:@1];
    [countedSet addObject:@2];
    [countedSet addObject:@2];
    NSLog(@"%@",countedSet);
單獨獲取某個對象在集合中出現過多少次
    NSInteger countOfObjec = [countedSet countForObject:@1];
    NSLog(@"%ld",countOfObjec);
 
通過快速枚舉來遍歷數組元素
    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);
    }
快速遍歷字典
直接遍歷字典直接得到的是字典的每一個鍵,可以通過遍歷得到的鍵去獲取對應的值
    for (NSString *key in dic1) {
        NSLog(@"dictionary[%@]:%@",key,dic1[key]);
    }
//    dic1[key]就相當於[dic1 objectForKey:key]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM