OC ---- 字典集合 iOS學習-----細碎知識點總結

來源:互聯網
上載者:User

標籤:

執行個體方法的建立    NSDictionary *wukong = [[NSDictionary alloc] initWithObjectsAndKeys:@"悟空", @"name", @"男", @"gender", @"500", @"age", nil ];    NSLog(@"%@", wukong);        // 便利構造器建立    NSDictionary *wuNeng = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"悟能", @"男", @"300", nil] forKeys:[NSArray arrayWithObjects:@"name", @"gender", @"age", nil]];    NSLog(@"wuNeng = %@", wuNeng);
// 擷取字典在所有key    NSArray *allKey = [wuNeng allKeys];    NSLog(@"allKey = %@", allKey);    // 擷取字典中所有的value    NSArray *allValue = [wuNeng allValues];    NSLog(@"allValue = %@", allValue);        // 通過Key查詢對應的value    NSString *name = [wukong valueForKey:@"name"];    NSLog(@"name = %@", name);        NSString *gender = [wukong valueForKey:@"gender"];    NSLog(@"gender = %@", gender);    // 從字典中根據key取value可以使用valueForKey和objectForKey    NSString *age = [wukong objectForKey:@"age"];    NSLog(@"%@", age);

#pragma mark - NSMutableDictionary

NSMutableDictionary *mingRen = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"漩渦鳴人", @"name", @"男", @"gender", @"19", @"age", nil];        // 添加索引值對    [mingRen setValue:@"吃拉麵" forKey:@"hobby"];    [mingRen setValue:@"木葉" forKey:@"address"];        // 更改    [mingRen setValue:@"吃包子" forKey:@"hobby"];        // setValue:forKey:和object:forkey:當key存在的時候,是修改key對應的value,當key不存在的時候是添加一對jianzhi    [mingRen setValue:nil forKey:@"hobbby"]; //    //[mingRen setValue:nil forKey:@"address"];  // 該方法中對應的object不可為空,否則會崩潰    //[mingRen setValue:[NSNull null] forKey:@"address"];  // 如果想給空值i個個、、、的話使用NSNull    //[mingRen setObject:[NSNull null] forKey:@"address"]        // 刪除    [mingRen removeObjectForKey:@"address"];        // for in快速枚舉 字典的快速枚舉,in前面是key的類型,in後面是字典對象(執行個體)    for (NSString *key in mingRen) {        NSLog(@"key = %@, value = %@", key, [mingRen valueForKey:key]);    }

#pragma mark - NSSet &NSMutableSet

// 建立一個NSSet對象    NSNumber *number1 = [NSNumber numberWithInt:3];    NSNumber *number2 = [NSNumber numberWithInt:10];    NSNumber *number3 = [NSNumber numberWithInt:20];    NSNumber *number4 = [NSNumber numberWithInt:30];    NSSet *set = [[NSSet alloc] initWithObjects:number1, number2, number3,number1, number4, nil];    NSLog(@"----%@", set);    NSLog(@" %ld", [set count]);  // 集合裡面的個數        NSLog(@"===%@", [set anyObject]);  // 從集合裡取出一個元素        if ([set containsObject:number1]) { // 集合中是否包含某個元素        NSLog(@"集合裡有number1這個對像");    } else {        NSLog(@"集合中沒有這個對象");    }        // 建立一個NSMutbleSet對象    NSMutableSet *mutableSet = [NSMutableSet setWithObjects:@3, @4, @5, @9, @20, nil];  // 字面量    NSLog(@"%@", [[set anyObject] class]);    NSLog(@"==%@", [number1 class]);        // 添加    [mutableSet addObject:@80];    // 刪除元素    [mutableSet removeObject:@3];    NSLog(@"-=-=%@", mutableSet);        NSCountedSet *countedSet = [NSCountedSet setWithObjects:number1, number2, number3 , number3, number4, number2, nil];    NSLog(@"%@", countedSet);    NSLog(@"number1的次數是:%ld", [countedSet countForObject:number1]); // 擷取某個對象在NSCountedSet中重複的次數        // 使用forin 對NSSet經行快速枚舉    for (NSNumber *number in set) {        NSLog(@"----%@", number);    }        // 從檔案讀取字串    NSError *error = nil;    NSString *txtString = [[NSString alloc] initWithContentsOfFile:@"/Users/lanou3g/Desktop/test.txt" encoding:NSUTF8StringEncoding error:&error];    NSLog(@"%@", txtString);

 

OC ---- 字典集合 iOS學習-----細碎知識點總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.