且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

循环通过字典迅速

更新时间:2022-10-17 21:27:27

这不是通过字典的循环,而是循环存储在其中一个字典中的数组键,如果你有一个数组,这是什么想要做的作为您的字典键之一。

 如果让arr = dict [Files]为? [String] {
for arr {

}
}

如果你确实想通过字典循环,这是可以在Swift,可以这样做:

  for dict {
println(key is - \(key)and value is - \(value))
}


I'm trying to replicate a for in loop I did in Objective C but am getting an "'AnyObject' does not have a member named 'GeneratorType' error:

 for (NSString *file in [dict objectForKey:@"Files"])
            {
    NSString *content = [[source stringByAppendingPathComponent:@"Contents"] stringByA
            }

Here is my Swift

 for file in dict.objectForKey("Files") {
                let content:String = source.stringByAppendingPathComponent("Contents").stringByAppendingPathComponent(file)
                }

I've tried defining a holder variable for dict. Can anyone see what I'm doing wrong, please?

This isn't a loop through a dictionary. It's looping though an array stored in one of the dictionaries keys. This is what would want to do if for example you had an array of strings as one of your dictionary's keys.

if let arr = dict["Files"] as? [String] {
    for file in arr {

    }
}

If you do want to just loop through the dictionary though, this is possible in Swift, and can be done like this:

for (key, value) in dict {
    println("key is - \(key) and value is - \(value)")
}