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

标签 swift for-loop iterator

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

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/

相关文章:

swift - 使用控件将 spritenode 移动一圈

ios - Xcode UIView 随机调整大小

python - 列表中连续对的总和,包括最后一个元素与第一个元素的总和

ios - 无法将 UISearchController 搜索栏添加到导航栏中,并且委托(delegate)方法未被调用

C# .net For() 步骤?

javascript - 最后一次迭代返回 undefined

java - 在迭代时将项目添加到链表是否安全

c++ - std::unordered_map<int, std::vector<Element>> 上的迭代器

c++ - 为什么这里不考虑 std::begin/end?

ios - 如何在大型导航栏上使用图像作为标题?