OC ---- 字典集合 iOS学习-----细碎知识点总结

实例方法的创建
    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);

 

posted @ 2015-11-07 19:21  小芳姑娘  Views(178)  Comments(0Edit  收藏  举报