zoukankan      html  css  js  c++  java
  • swift基础语法(10-字典)

    定义字典
    OC:
    NSDictionary *dict =[NSDictionary dictionaryWithObject:
                                      @“qbs" forKey:@"name"];
    NSLog(@"%@", dict);
    输出结果:
    2016-01-06 15:09:11.214 OCTest[3773:761032] {
        name = gxq;
    }
    NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys:
                                      @"name", @"qbs",
                                      @"age", @20, nil];
    NSLog(@"%@", dict);
    输出结果:
    2016-01-06 15:13:39.853 OCTest[3831:792730] {
        gxq = name;
        20 = age;
    }
    NSDictionary *dict = @{@"name":@"qbs", @"age":@20};
    NSLog(@"%@", dict);
    输出结果:
    2016-01-06 15:14:57.616 OCTest[3841:801710] {
        age = 20;
        name = gxq;
    }
    swift:
    key一定要是可以hash的(String, Int, Float, Double, Bool)
    value没有要求
    var dict = ["name":"qbs", "age":20]
    print(dict)
    var dict1:Dictionary = ["name":"qbs", "age":20]
    print(dict1)
    var dict2:Dictionary<String,AnyObject> = ["name":"qbs", "age":20]
    print(dict2)
    var dict3:[String:AnyObject] = ["name":"qbs", "age":20]
    print(dict3)
    var dict4:[String:AnyObject] =
    Dictionary(dictionaryLiteral: ("name", "qbs"), ("age", 20))
    print(dict4)
    输出结果:
    ["age": 20, "name": qbs]
    ["age": 20, "name": qbs]
    ["age": 20, "name": qbs]
    ["age": 20, "name": qbs]
    ["age": 20, "name": qbs]
     
    可变字典:
    var dict5 = [:]
    不可变字典:
    let dict6  = [:]
     
    字典操作
    OC:
    获取
    NSDictionary *dict = @{@"name":@"qbs", @"age":@20};
    NSLog(@"%@", dict[@"name"]);
    输出结果:
    2016-01-06 15:26:00.351 OCTest[3923:881138] gxq
     
    修改
    NSMutableDictionary *dict =
    [NSMutableDictionary dictionaryWithObjectsAndKeys:
                         @"name", @"qbs",
                         @"age", @20, nil];
    dict[@"name"] = @"iversion";
    NSLog(@"%@", dict[@"name"]);
     
    NSMutableDictionary *dict =
    [NSMutableDictionary dictionaryWithObjectsAndKeys:
                        @"name", @"qbs",
                        @"age", @20, nil];
    [dict setObject:@"iversion" forKey:@"name"];
    NSLog(@"%@", dict[@"name"]);
     
    输出结果:
    2016-04-01 15:27:01.704 OCTest[3933:890317] iversion
    2016-04-01 15:28:21.398 OCTest[3943:899613] iversion
     
    swift:
    获取
    var dict = ["name":"qbs", "age":20]
    print(dict["name"]!)
    输出结果: gxq
     
     
    修改
    var dict = ["name":"qbs", "age":20]
    dict["name"] = "iverson"
    print(dict["name"]!)
    var dict1 = ["name":"qbs", "age":20]
    dict1.updateValue("iverson", forKey: "name")
    print(dict["name"]!)
    输出结果:
    iverson
    iverson
     
    更新
    updateValue返回一个可选类型
    如果字典中不存在需要更新的key, 那么返回nil, 如果存在返回原始值
    var dict = ["name":"qbs", "age":25]
    if let orignal = dict.updateValue("iverson", forKey: "name")
    {
        print(dict["name"]!)
        print(orignal)
    }
    输出结果:
    iverson
    qbs
     
    updateValue返回一个可选类型
    如果字典中不存在需要更新的key, 那么返回nil并且会将新的键值对添加到字典中
    var dict = ["name":"qbs", "age":25]
    if let orignal = dict.updateValue("iverson", forKey: "abc")
    {
        print(dict["abc"]!)
        print(orignal)
    }
    print(dict)
    输出结果: ["abc": iverson, "age": 25, "name": qbs]
    添加
    OC:
    NSMutableDictionary *dict =
    [NSMutableDictionary dictionaryWithObjectsAndKeys:
                       @"name", @"qbs",
                       @"age", @25, nil];
    dict[@"height"] = @100;
    NSLog(@"%@", dict);
    输出结果:
    2016-04-01 15:35:11.734 OCTest[4025:946250] {
        qbs = name;
        25 = age;
        height = 100;
    }
     
    NSMutableDictionary *dict =
    [NSMutableDictionary dictionaryWithObjectsAndKeys:
                        @"name", @“QBS",
                         @"age", @30, nil];
    [dict setObject:@200 forKey:@"height"];
    NSLog(@"%@", dict);
    输出结果:
    2016-04-01 15:36:15.768 OCTest[4035:953931] {
        QBS = name;
        30 = age;
        height = 200;
    }
     
    swift:
    var dict = ["name:"Qbs", "age":50]
    dict["height"] = 160;
    print(dict)
    输出结果: ["height": 160, "age": 50, "name": Qbs]
     
     
    删除
    OC:
    NSMutableDictionary *dict =
    [NSMutableDictionary dictionaryWithObjectsAndKeys:
                         @"qbs",@"name",
                         @30,@"age",nil];
    [dict removeObjectForKey:@"name"];
    NSLog(@"%@", dict);
    输出结果:
    2016-04-01 15:40:37.801 OCTest[4058:981747] {
        age = 30;
    }
     
    NSMutableDictionary *dict =
    [NSMutableDictionary dictionaryWithObjectsAndKeys:
                         @“qbs",@"name",
                         @30,@"age",nil];
    [dict removeAllObjects];
    NSLog(@"%@", dict);
    输出结果:
    2016-04-01 15:41:20.705 OCTest[4068:989096] {
    }
    swift:
     
    var dict = ["name":"qbs", "age":10]
    dict.removeValueForKey("name")
    print(dict)
    输出结果: ["age": 10]
     
    removeValueForKey返回一个可选类型
    如果字典中不存在需要删除的key, 那么返回nil并且不会执行任何操作
    如果存在则删除key对应的值, 并且返回被删除的值
    var dict = ["name:"abs", "age:20]
    if let orignal = dict.removeValueForKey("names")
    {
        print(dict)
        print(orignal)
    }
    print(dict)
    输出结果:["age": 30, "name": qbs]
     
    var dict = ["name:”qbs", "age":30]
    dict.removeAll(keepCapacity: true)
     
    遍历字典
    OC:
    NSMutableDictionary *dict =
    [NSMutableDictionary dictionaryWithObjectsAndKeys:
                         @"qbs",@"name",
                         @40,@"age",nil];[dictenumerateKeysAndObjectsUsingBlock:^(idkey,idobj,BOOL*stop) {
          NSLog(@"key = %@ value = %@", key, obj);
    }];
    输出结果:
    2016-04-01 15:45:59.810 OCTest[4117:1022823] key = name value = qbs
    2016-04-01 15:45:59.811 OCTest[4117:1022823] key = age value = 40
     
    NSMutableDictionary *dict =
    [NSMutableDictionary dictionaryWithObjectsAndKeys:
                         @“qbs", @"name",
                         @40, @"age", nil];
    NSArray *keys = [dict allKeys];
    for (NSString *key in keys) {
        NSLog(@"%@", key);
    }
    输出结果:
    2016-04-01 15:48:07.861 OCTest[4137:1039198] name
    2016-04-01 15:48:07.862 OCTest[4137:1039198] age
    NSMutableDictionary *dict =
    [NSMutableDictionary dictionaryWithObjectsAndKeys:
                         @"qbs", @"name",
                         @40, @"age", nil];
    NSArray *keys = [dict allValues];
    for (NSString *key in keys) {
        NSLog(@"%@", key);
    }
    输出结果:
    2016-04-01 15:49:20.375 OCTest[4148:1049548] gxq
    2016-04-01 15:49:20.376 OCTest[4148:1049548] 40
     
     
    var dict1 = ["name:"Qbs", "age":30]
    for (key , value) in dict1
    {
        print("key = (key) value = (value)")
    }
    输出结果:
    key = age value = 30
    key = name value = Qbs
     
     
    var dict2 = ["name:"Qbs", "age":40]
    for key in dict2.keys
    {
        print("key = (key)")
    }
    输出结果:
    key = age
    key = name
     
    var dict3 = ["name:"Qbs", "age":50]
    for value in dict3.values
    {
        print("value = (value)")
    }
    输出结果:
    value = 50
    value = Qbs
     
     
     
     
     
     
     
    我们每一种习惯都是由一再重复的行为所铸造的,因此,优秀不是一种行为,而是一种习惯.
  • 相关阅读:
    python下RSA 加密/解密,签名/验证
    python字符串str和字节数组相互转化
    代码存档
    windows 7 安装 scrapy
    scrapy 爬取自己的博客
    win32api 找不到指定的模块
    SQLite3日期与时间,常见函数
    sqlite3日期数据类型
    myeclipse集成maven
    UIKit class hierarchy
  • 原文地址:https://www.cnblogs.com/jordanYang/p/5378239.html
Copyright © 2011-2022 走看看