Loop through a dictionary in swift

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

  • Swift dictionary get the value without access or knowing the key
  • How to use native Swift way to Read Plist in Swift 3?
  • Casting NSDictionary as Dictionary Swift
  • Swift dictionary bug?
  • Post dictionary from Swift to PHP
  • Dictionary (key , value) order
  •  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?

    Solutions Collect From Internet About “Loop through a dictionary in swift”

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