gpt4 book ai didi

Swift for 循环 "Expression type ' [[String : String]]' is ambiguous without more context

转载 作者:搜寻专家 更新时间:2023-10-31 22:06:43 26 4
gpt4 key购买 nike

我正在尝试从字典数组中访问以下项目,但我遇到了两个问题(都是不同的方法)。字典数组初始化如下:

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"])

第一种方法:

for (k,v) in testingArray {
// code here
}

由于(出现在 for 循环已初始化的行上)而无法运行:

"Expression type '[[String : String]]' is ambiguous without more context

第二种方法:

for indices in testingArray {
for(k, v) in indices {
print(indices.keys)
}
}

返回以下内容:

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))

LazyMapCollection<Dictionary<String, String>, String>(_base: ["url": "localhost:8088", "name": "Bugatti"], _transform: (Function))

LazyMapCollection<Dictionary<String, String>, String>(_base: ["url": "localhost:8088", "name": "Bugatti"], _transform: (Function))

LazyMapCollection<Dictionary<String, String>, String>(_base: ["url": "localhost:8088", "name": "Brazil"], _transform: (Function))

LazyMapCollection<Dictionary<String, String>, String>(_base: ["url": "localhost:8088", "name": "Brazil"], _transform: (Function))

LazyMapCollection<Dictionary<String, String>, String>(_base: ["url": "localhost:8088", "name": "Jasmine"], _transform: (Function))

LazyMapCollection<Dictionary<String, String>, String>(_base: ["url": "localhost:8088", "name": "Jasmine"], _transform: (Function))

LazyMapCollection<Dictionary<String, String>, String>(_base: ["url": "localhost:8088", "name": "Hello"], _transform: (Function))

LazyMapCollection<Dictionary<String, String>, String>(_base: ["url": "localhost:8088", "name": "Hello"], _transform: (Function))

这是我试图实现的等效伪代码:

for(int i = 0; i < sizeOfArray; i++ {
print testingArray[i]."name"
print testingArray[i]."url"
}

我已经为此绞尽脑汁好几天了,但我对 swift 及其习语的了解还不足以单独解决这个问题,我们将不胜感激任何帮助(尤其是如果我们能弄清楚如何让#1 工作) .

最佳答案

我同意错误消息令人困惑/误导。但是 for (k,v) in testingArray 没有意义,因为 testingArray 是数组,不是字典。它的元素是字典。

我认为您正在寻找这样的东西:

for obj in testingArray {
print(obj["name"])
print(obj["url"])
}

关于Swift for 循环 "Expression type ' [[String : String]]' is ambiguous without more context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36367362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com