在Swift 3游乐场中,我想创建一个可变数组,其中包含一些索引处的数字和其他索引处的字符串。我这样做会出错:
var playerInfo = [[String]]()
playerInfo[0][2] = "Adam" //Error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)
playerInfo[0][3] = "Martinez"
playerInfo[0][1] = "00"
编辑: 像这样的东西?
var playerInfo: [[String:Any]] = [
[
"playerNumber" : "00",
"playerFirstName" : "Adam",
"playerLastName" : "Martinez"
]
]
答案 0 :(得分:1)
您没有正确使用语法。
试试这个:
var playerInfo = [[String]]()
playerInfo.append(["00","Adam","Martinez"])
修改强>
对于您的编辑,您需要一个词典:
var playerInfo = Dictionary<String, Any>()
playerInfo["playerNumber"] = "00"
playerInfo["playerFirstName"] = "Adam"
playerInfo["playerLastName"] = "Martinez"