Swift

//: Playground - noun: a place where people can play

import UIKit

// 注意: swift中的字典用的也是中括号, 和OC的大括号是不同的

// 初始化字典
var dict1 = [1:"one", 2:"two", 3:"three"]   // 隐式
//var dict1:Dictionary<Int, String> = [1:"one", 2:"two", 3:"three"]  //显式
//var dict1:[Int:String] = [1:"one", 2:"two", 3:"three"]

var dict2 = ["a":"Rinpe", "b":"Bobo", "c":"Lili"]
//var dict2:Dictionary<String, String> = ["a":"Rinpe", "b":"Bobo", "c":"Lili"]
//var dict2:[String:String] = ["a":"Rinpe", "b":"Bobo", "c":"Lili"]

// 根据Key找到value
dict1[1]
dict2["a"]

// 创建一个空的字典
var nullDict = Dictionary<String, String>()
var nullDict2 = [String:String]()

// 清空一个字典
dict1 = [:]
dict1 = [Int : String]();

  

原文地址:https://www.cnblogs.com/Rinpe/p/5050805.html