在Swift中初始化字典的空数组 [英] Initialising empty arrays of dictionaries in Swift
问题描述
对于一个字符串数组,它很简单:
var myStringArray:String [] = []
myStringArray + =a
myStringArray + =b
- > [a,b]
和整数
var myIntArray:Int [] = []
myIntArray + = 1
myIntArray + = 2
- > ; [1,2]
它还适用于其他类型的对象,如NSImage对象:
let path =/ Library / Application Support / Apple / iChat图标/标志/
let image1 = NSImage(byReferencingFile:路径+Brazil.png)
let image2 = NSImage(byReferencingFile:path +Chile.png)
var myImageArray:NSImage [] = []
myImageArray + = image1
myImageArray + = image2
- > [< NSImage 0x7fe371c199f0 ...>,< NSImage 0x7fe371f39ea0 ...>]
但是,我无法使用语法来初始化一个空的数组字典。
我知道您可以拥有一系列的字典,因为初始化具有初始值:
let myDict1 = [someKey:someValue]
let myDict2 = [anotherKey:anotherValue]
var myDictArray = [myDict1]
myDictArray + = myDict2
- > [[someKey:someValue],[anotherKey:anotherValue]]
编辑:在下面的代码中有一个错误(混合myNewDictArray和myDictArray),当更正它不工作,就像我说的那样。
这样做:
let myDict1 = [someKey:someValue]
let myDict2 = [anotherKey:anotherValue]
var myNewDictArray:AnyObject = []
myDictArray + myDict1
myDictArray + = myDict2
- > [[someKey:someValue],[anotherKey:anotherValue]]
然而(你希望语法是)会失败:
var myNewDictArray:Dictionary [] = []
code>不能将表达式的类型'Dictionary []转换为'Hashable'类型
所以问题是什么是正确的初始化一个空数组的字典项目的方式,为什么这个语法不是 var myNewDictArray:Dictionary [] = []
work?
你需要给字典类型:
var myNewDictArray :[Dictionary< String,Int>] = []
或
var myNewDictArray = [Dictionary< String,Int>]()
编辑:您也可以使用较短的语法:
var myNewDictArray:[[String:Int]] = []
或
var myNewDictArray = [[String:Int]]()
I'm trying to wrap my head around initialising empty arrays in Swift.
For an array of strings it's pretty straight forward :
var myStringArray: String[] = []
myStringArray += "a"
myStringArray += "b"
-> ["a", "b"]
and for integers
var myIntArray: Int[] = []
myIntArray += 1
myIntArray += 2
-> [1, 2]
it also works for other types of object such as NSImage objects :
let path = "/Library/Application Support/Apple/iChat Icons/Flags/"
let image1 = NSImage(byReferencingFile: path + "Brazil.png")
let image2 = NSImage(byReferencingFile: path + "Chile.png")
var myImageArray: NSImage[] = []
myImageArray += image1
myImageArray += image2
-> [<NSImage 0x7fe371c199f0 ...>, <NSImage 0x7fe371f39ea0 ...>]
However I can't work out the syntax to initialise an empty array of Dictionaries.
I know you can have an array of Dictionaries because initialising with an initial value works :
let myDict1 = ["someKey":"someValue"]
let myDict2 = ["anotherKey":"anotherValue"]
var myDictArray = [myDict1]
myDictArray += myDict2
-> [["someKey": "someValue"], ["anotherKey": "anotherValue"]]
EDIT : There was an error in the following code (mixing myNewDictArray and myDictArray) and when corrected it doesn't work as I said it did.
as does this :
let myDict1 = ["someKey":"someValue"]
let myDict2 = ["anotherKey":"anotherValue"]
var myNewDictArray: AnyObject = []
myDictArray += myDict1
myDictArray += myDict2
-> [["someKey": "someValue"], ["anotherKey": "anotherValue"]]
This however (which you'd expect the syntax to be) fails :
var myNewDictArray: Dictionary[] = []
with the error Cannot convert the expression's type 'Dictionary[]' to type 'Hashable'
So the question is what is the correct way to initialise a empty array of Dictionary Items and why doesn't this syntax var myNewDictArray: Dictionary[] = []
work?
You need to give types to the dictionaries:
var myNewDictArray: [Dictionary<String, Int>] = []
or
var myNewDictArray = [Dictionary<String, Int>]()
Edit: You can also use the shorter syntax:
var myNewDictArray: [[String:Int]] = []
or
var myNewDictArray = [[String:Int]]()
这篇关于在Swift中初始化字典的空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!