我试图从一个字典数组访问以下项目,我有两个问题(两者都是不同的方法).字典数组初始化如下:
var testingArray = [[String: String]()]
testingArray.append(["name": "Ethiopia", "url": "localhost:8088"])
testingArray.append(["name": "Bugatti", "url": "localhost:8088"])
testingArray.append(["name": "Brazil", "url": "localhost:8088"])
testingArray.append(["name": "Jasmine", "url": "localhost:8088"])
testingArray.append(["name": "Hello", "url": "localhost:8088"])
Run Code Online (Sandbox Code Playgroud)
第一种方法:
for (k,v) in testingArray {
// code here
}
Run Code Online (Sandbox Code Playgroud)
由于(在for循环初始化的行上出现)将无法运行:
"Expression type '[[String : String]]' is ambiguous without more context
Run Code Online (Sandbox Code Playgroud)
第二种方法:
for indices in testingArray {
for(k, v) in indices {
print(indices.keys)
}
}
Run Code Online (Sandbox Code Playgroud)
返回以下内容:
LazyMapCollection<Dictionary<String, String>, String>(_base: ["url": "localhost:8088", "name": "Ethiopia"], _transform: (Function))
LazyMapCollection<Dictionary<String, String>, String>(_base: ["url": "localhost:8088", "name": "Ethiopia"], _transform: (Function)) …Run Code Online (Sandbox Code Playgroud)