Swift:声明一个空字典

我开始学习swift通过苹果提供的Swift上的iBook-The Swift Programming Language。书中说,要创建一个空字典,应该使用[:],就像声明数组为[]一样:

我声明一个空数组,如下所示:

let emptyArr = [] // or String[]()

但是在声明空字典时,我得到语法错误:

let emptyDict = [:]

如何声明一个空字典?

241002 次浏览
var emptyDictionary = [String: String]()

var populatedDictionary = ["key1": "value1", "key2": "value2"]

请注意:如果你计划随时间改变字典的内容,那么将其声明为变量(var)。你可以声明一个空字典为常量(let),但如果你想改变它,这将是毫无意义的,因为常量值在初始化后不能改变。

你必须给字典一个类型

// empty dict with Ints as keys and Strings as values
var namesOfIntegers = Dictionary<Int, String>()

如果编译器可以推断类型,则可以使用较短的语法

namesOfIntegers[16] = "sixteen"
// namesOfIntegers now contains 1 key-value pair
namesOfIntegers = [:]
// namesOfIntegers is once again an empty dictionary of type Int, String

除非类型信息可用,否则不能使用[:]

在这种情况下,你需要显式地提供它:

var dict = Dictionary<String, String>()

var表示它是可变的,所以你可以向它添加条目。 相反,如果你使它为let,那么你不能进一步修改它(let表示常量)

例如,如果类型信息可以推断,则可以使用[:]简写符号

var dict = ["key": "value"]


// stuff


dict = [:] // ok, I'm done with it

在上一个例子中,通过第一行就知道字典的类型为Dictionary<String, String>。注意,您不必显式地指定它,但它已经被推断出来了。

我也在玩这个。看起来很奇怪,你可以声明一个空字典,然后像这样添加一个键/值对:

var emptyDictionary = Dictionary<String, Float>()
var flexDictionary = [:]
emptyDictionary["brian"] = 4.5
flexDictionary["key"] = "value" // ERROR : cannot assign to the result of this expression

但是你可以通过使用"Any"类型创建一个接受不同值类型的Dictionary,如下所示:

var emptyDictionary = Dictionary<String, Any>()
emptyDictionary["brian"] = 4.5
emptyDictionary["mike"] = "hello"

你可以像这样简单地声明它:

var emptyDict:NSMutableDictionary = [:]

用这个会有用的。

var emptyDict = [String: String]()

它很方便你找路

var dict:Dictionary = [:]

var dictList = String: swift字典的字符串 var arrSectionTitle = swift中数组的字符串

. var

斯威夫特的文档建议使用以下方法初始化一个空Dictionary:

var emptyDict = [String: String]()

当我第一次遇到这个问题时,我有点困惑,因为不同的答案显示了初始化空Dictionary的不同方法。事实证明,实际上有很多方法可以做到这一点,尽管考虑到Swift推断类型的能力,有些方法有点冗余或过于冗长。

var emptyDict = [String: String]()
var emptyDict = Dictionary<String, String>()
var emptyDict: [String: String] = [:]
var emptyDict: [String: String] = [String: String]()
var emptyDict: [String: String] = Dictionary<String, String>()
var emptyDict: Dictionary = [String: String]()
var emptyDict: Dictionary = Dictionary<String, String>()
var emptyDict: Dictionary<String, String> = [:]
var emptyDict: Dictionary<String, String> = [String: String]()
var emptyDict: Dictionary<String, String> = Dictionary<String, String>()

在你有一个空字典之后,你可以像这样添加一个键值对:

emptyDict["some key"] = "some value"

如果你想再次清空你的字典,你可以做以下事情:

emptyDict = [:]

类型仍然是<String, String>,因为它是这样初始化的。

你可以用以下方法将其声明为nil:

var assoc : [String:String]

好的是你已经有了排版(注意我用了var和not let,把它们看成可变和不可变)然后你可以稍后再填写:

assoc = ["key1" : "things", "key2" : "stuff"]

迅速:

var myDictionary = Dictionary<String, AnyObject>()
var parking = [Dictionary < String, Double >()]

^这将为[string:double]输入添加一个字典

当你在Swift中声明任何东西时,你需要显式地告诉数据类型或类型可以推断。

斯威夫特3

下面的示例声明了一个字典,其中key类型为Int, value类型为String

方法1:初始化器

let dic = Dictionary<Int, String>()

方法二:简写语法

let dic = [Int:String]()

方法3:字典直译

var dic = [1: "Sample"]
// dic has NOT to be a constant
dic.removeAll()

如果要创建具有任何类型的泛型字典

var dictionaryData = [AnyHashable:Any]()

我通常用

var dictionary:[String:String] = [:]
dictionary.removeAll()

斯威夫特4

let dicc = NSDictionary()


//MARK: - This is empty dictionary
let dic = ["":""]


//MARK:- This is variable dic means if you want to put variable
let dic2 = ["":"", "":"", "":""]


//MARK:- Variable example
let dic3 = ["name":"Shakeel Ahmed", "imageurl":"https://abc?abc.abc/etc", "address":"Rawalpindi Pakistan"]


//MARK: - This is 2nd Variable Example dictionary
let dic4 = ["name": variablename, "city": variablecity, "zip": variablezip]


//MARK:- Dictionary String with Any Object
var dic5a = [String: String]()
//MARK:- Put values in dic
var dic5a = ["key1": "value", "key2":"value2", "key3":"value3"]


var dic5b = [String:AnyObject]()
dic5b = ["name": fullname, "imageurl": imgurl, "language": imgurl] as [String : AnyObject]


or
//MARK:- Dictionary String with Any Object
let dic5 = ["name": fullname, "imageurl": imgurl, "language": imgurl] as [String : AnyObject]


//MARK:- More Easy Way
let dic6a = NSDictionary()
let dic6b = NSMutalbeDictionary()

您可以使用以下代码:

var d1 = Dictionary<Int, Int>()
var d2 = [Int: Int]()
var d3: Dictionary<Int, Int> = [Int : Int]()
var d4: [Int : Int] = [:]

宣布,在Swift中初始化字典

字符串字典

var stringDict: [String: String] = [String: String]()

var stringDict: Dictionary<String, String> = Dictionary<String, String>()

Int字典

var stringDict: [String: Int] = [String: Int]()

var stringDict: Dictionary<String, Int> = Dictionary<String, Int>()

AnyObject字典

var stringDict: [String: AnyObject] = [String: AnyObject]()

var stringDict: Dictionary<String, AnyObject> = Dictionary<String, AnyObject>()

字符串数组字典

var stringDict: [String: [String]] = [String: [String]]()

var stringDict: Dictionary<String, Array<String>> = Dictionary<String, Array<String>>()

字符串字典数组

var stringDict: [[String: String]] = [[String: String]]()

var stringDict: Array<Dictionary<String, String>> = Array<Dictionary<String, String>>()

要使用[:] aka the empty dictionary literal创建一个空字典,实际上需要首先提供上下文,即键和值的类型。使用[:]创建空字典的正确方法是:

var dict: [String: Int] = [:]