字典的行使

/*

/*

  • 一.字典的介绍
    字典允许依照某些键来访问成分
    字典是由两局地集合构成的,三个是键(key)集合,三个是值(value)集合
    键集合是不可能有重复成分的,而值集合是可以重复的,键和值是成对出现的
    Swift字典类型是Dictionary,也是1个泛型集合

  • 二.字典的早先化

    • 1.Swift中的可变和不可变字典
      运用let修饰的数组是不可变字典
      采纳var修饰的数组是可变字典

    • 2.概念四个可变字典
      var dict:[String : NSObject] = String :
      NSObject

    • 3.概念2个不足变字典
      let dict2 = [“name” : “why”, “age” : 18]

    • 4.在声喜宝(Hipp)(Beingmate)个Dictionary类型的时候能够动用下边包车型大巴语句之壹
      var dict1: Dictionary<Int, String>
      var dict2: [Int: String]

    • 5.评释的字典需求实行开端化才能利用,字典类型往往是在宣称的还要拓展初叶化的
      // 定时字典的还要,实行开首化
      var dict = [“name” : “why”, “age” : 18]
      // swift中4意对象,平时不行使NSObject,使用AnyObject
      var dict : Dictionary<String,AnyObject>
      dict = [“1″:”2″ as AnyObject,”3″:”4” as AnyObject]

  • 三.字典的基本操作

    • 一.创办三个字典
      let dic = [“name”:”王冲”,”age”:”25″]

    澳门葡京备用网址 1

    始建三个字典

    • 2.遍历字典,利用元祖类型便利字典会将字典里的key值赋值给元祖里面包车型大巴第1个变量,讲value赋值给元祖里面第一个变量,提示:第叁个是key,首个是value
      for (k,v) in dic {
      print(k,v)
      }

    • 3.新增key/value,新增key/value,假如对应的key在字典里面未有就会激增
      var dict = [“name”:”王冲”,”age”:”25″,”sex”:”男”]
      // 新增成分
      dict[“love”] = “读书”

    • 4.更新key/value,假如对应的key在字典里面有就会更新

       dict["age"] = "26"
      
    • 5.刨除某四个key和value
      dict.removeValue(forKey: “name”)

    • 陆.查询某三个值
      dict[“name”]

  • 四.字典的遍历(key遍历value遍历字典的行使。,元祖遍历)

    • 一.字典键的遍历
      for key in dict.keys {

            print(key)
      }
      
    • 2.字典值的遍历

      for value in dict.values {
      
           print(value)
      }
      
    • 三.字典元祖的遍历
      for (k,v) in dict {

         print(k,v)
      }
      
  • 五.字典的联结

    var dict1 = ["name":"王冲","age":"25"]
    var dict2 = ["sex":"男"]
    
    for (key,value) in dict1 {
    
         dict2[key] = value
    }
    

字典

swift
字典不仅可以储存对象类型的值,还足以储存宗旨数据类型的值,结构体,枚举值。字典本质上也是结构体。

  • 字典证明
    let dic1 = [“key1”:1, “key2”:2, “key3”:3, “key4”:4]
    let dic2 = String:Int
    let dic3:[String:Int] = [“key1”:1, “key2”:2, “key3”:3,
    “key4”:4]
    let dic4:Dictionary = [“key1″:1,”key2”:2];
    // 字典范性定义的法子
    let dic5:Dictionary<String, Int> = [“key1”:1, “key2”:2,
    “key3”:3]
    let dic6:[String:AnyObject] = [“key1”:1 as AnyObject, “key2”:2 as
    AnyObject, “key3″:”value3” as AnyObject, “key4”:4 as AnyObject]
    let dic7:NSDictionary = NSDictionary(dictionary: [“key1”:1,
    “key2”:2, “key3”:3, “key4”:4])
    //使用init()构造器
    let dic8:[String:String] = Dictionary<String,String>()
    //使用init(minimumCapacity:Int) 并且钦点了字典的细小容积
    let dic9:[String: Int] = Dictionary<String,
    Int>(minimumCapacity: 5)
  • 字典3月素的个数
    print(dic3.count)
    print(dic2.count)
  • 字典是不是为空
    print(dic1.isEmpty)
  • 获取字典中的key/value
    print(dic7.allKeys)
    print(dic7.allValues)
    print(dic4[“key1”]!)
    print(dic5[“key2”]!)
  • 修改字典
    var dic10:Dictionary<String,String>
    dic10 = [“key1″:”value1″,”key2″:”value2”]
    dic10[“key1”] = “changeString”
    print(dic10)
  • 加多字典
    var dic10:Dictionary<String,String>
    dic10 = [“key1″:”value1″,”key2″:”value2”]
    dic10[“key3”] = “value3”
    dic10.updateValue(“value”, forKey: “key4”)
  • 删除
    var dic10:Dictionary<String,String>
    dic10 = [“key1″:”value1″,”key2″:”value2”]
    // 删除钦赐key
    dic10[“key1”] = nil
    dic10.removeValue(forKey: “key2”)
    // 删除全体
    dic10 = [:]
    dic10.removeAll()
    // 对于 keepCapacity :false /true ,遵照供给接纳就可以;差距是true的话,会保持数据体量,占据空间?
    dic10.removeAll(keepingCapacity: true)

 字典定义: 键值对

 字典定义: 键值对

遍历字典

   for (key, value) in dic3 {
      print(key,value)
  }
  for key in dic3.keys {
      print(key)
  }
  for value in dic3.values {
      print(value)
  }

 OC:

 OC:

 NSDictionary *dict = [NSDictionary dictionaryWithObject:@”hjq”
forKey:@”name”];

 NSDictionary *dict = [NSDictionary dictionaryWithObject:@”hjq”
forKey:@”name”];

 NSLog(@”%@”, dict);

 NSLog(@”%@”, dict);

 

 

 NSDictionary *dict = [NSDictionary
dictionaryWithObjectsAndKeys:@”name”, @”hjq”, @”age”, @30, nil];

 NSDictionary *dict = [NSDictionary
dictionaryWithObjectsAndKeys:@”name”, @”hjq”, @”age”, @30, nil];

 NSLog(@”%@”, dict);

 NSLog(@”%@”, dict);

 

 

 NSDictionary *dict = @{@”name”:@”hjq”, @”age”:@25};

 NSDictionary *dict = @{@”name”:@”hjq”, @”age”:@25};

 NSLog(@”%@”, dict);

 NSLog(@”%@”, dict);

 */

 */

 

 

// key一定如若能够hash的(String, Int, Double, Bool), value未有须要

// key一定假使能够hash的(String, Int, Double, Bool), value没有供给

var dict = [“name”:”hjq”,”age”:25.5] as Any

var dict = [“name”:”hjq”,”age”:25.5] as Any

print(dict)

print(dict)

 

 

//var dict1:Dictionary = [“name”:”hjq”,”age”:25.5] 废弃

//var dict1:Dictionary = [“name”:”hjq”,”age”:25.5] 废弃

 

 

var dict2:Dictionary<String,AnyObject> = [“name”:”jq” as
AnyObject,”age”:25.5 as AnyObject]

var dict2:Dictionary<String,AnyObject> = [“name”:”jq” as
AnyObject,”age”:25.5 as AnyObject]

print(dict2)

print(dict2)

 

 

//var dict3:Dictionary<String:AnyObject> =
[“name”:”jq”,”age”:34]  废弃

//var dict3:Dictionary<String:AnyObject> =
[“name”:”jq”,”age”:34]  废弃

 

 

var dict4:[String:AnyObject] = [“name”:”hjq” as AnyObject ,”age”:30
as AnyObject ]

var dict4:[String:AnyObject] = [“name”:”hjq” as AnyObject ,”age”:30
as AnyObject ]

print(dict4)

print(dict4)

 

 

//var dict5:[String:AnyObject] = Dictionary(dictionaryLiteral:
(“name”,”hjq”),(“age”,28))

//var dict5:[String:AnyObject] = Dictionary(dictionaryLiteral:
(“name”,”hjq”),(“age”,28))

//print(dict5)  废弃

//print(dict5)  废弃

 

 

//3.0

//3.0

//一.字典的概念使用[key:value,key:value]火速定义

//一.字典的定义使用[key:value,key:value]快快定义

let dic:[String:Any] = [“name”:”zhang”,”age”:12]

let dic:[String:Any] = [“name”:”zhang”,”age”:12]

print(dic)

print(dic)

 

 

//数组字典

//数组字典

let arrDic:[[String:Any]] = [

let arrDic:[[String:Any]] = [

  [“name”:”hello”,”age”:22],

  [“name”:”hello”,”age”:22],

  [“name”:”hi”,”age”:23]

  [“name”:”hi”,”age”:23]

]

]

print(arrDic)

print(arrDic)

 

 

//二.可变字典的增加和删除改查

//二.可变字典的增加和删除改查

var dictionary:[String:Any] = [“name”:”hjq”,”age”:23]

var dictionary:[String:Any] = [“name”:”hjq”,”age”:23]

print(dictionary)

print(dictionary)

/** key存在则为修改, key不存在则为增添 **/

/** key存在则为修改, key不设有则为增多 **澳门葡京备用网址 ,/

//扩大键值对

//扩充键值对

dictionary[“score”] = 98

dictionary[“score”] = 98

print(dictionary)

print(dictionary)

 

 

//修改键值对

//修改键值对

dictionary[“age”] = 80

dictionary[“age”] = 80

print(dictionary)

print(dictionary)

 

 

//删除键值对

//删除键值对

dictionary.removeValue(forKey: “name”)

dictionary.removeValue(forKey: “name”)

print(dictionary)

print(dictionary)

 

 

//删除键值对

//删除键值对

// ps: 字典是通过key来稳定的, 全部的key必须是足以 hash/哈希 的
(MD5是壹种哈希, 哈希正是将字符串变成唯一的整数, 便于查找,
能抓好字典遍历的快慢)

// ps: 字典是通过key来恒定的, 全体的key必须是足以 hash/哈希 的
(MD5是1种哈希, 哈希正是将字符串变成唯一的整数, 便于查找,
能增高字典遍历的速度)

//dictionary.removeValue(forKey: <#T##String#>)

//dictionary.removeValue(forKey: <#T##String#>)

 

 

//字典遍历

//字典遍历

//写法一

//写法一

for e in dictionary

for e in dictionary

{

{

    print(“key = \(e.key) value = \(e.value)”)

    print(“key = \(e.key) value = \(e.value)”)

}

}

 

 

//写法二

//写法二

for (key,value) in dictionary

for (key,value) in dictionary

{

{

    print(“key = \(key) value = \(value)”)

    print(“key = \(key) value = \(value)”)

}

}

 

 

//字典合并

//字典合并

var dic5 = [“name”:”hihello”,”age”:23] as [String : Any]

var dic5 = [“name”:”hihello”,”age”:23] as [String : Any]

let dic6 = [“teacher”:”wang”]

let dic6 = [“teacher”:”wang”]

 

 

for (key,value) in dic6

for (key,value) in dic6

{

{

    dic5[key] = value

    dic5[key] = value

}

}

print(dic5)

print(dic5)

 

 

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*
*
Website