Iterating Through a Dictionary in Swift

后端 未结 7 670
醉话见心
醉话见心 2020-11-27 12:12

I am a little confused on the answer that Xcode is giving me to this experiment in the Swift Programming Language Guide:

// Use a for-in to iterate through a         


        
相关标签:
7条回答
  • 2020-11-27 12:26

    Dictionaries in Swift (and other languages) are not ordered. When you iterate through the dictionary, there's no guarentee that the order will match the initialization order. In this example, Swift processes the "Square" key before the others. You can see this by adding a print statement to the loop. 25 is the 5th element of Square so largest would be set 5 times for the 5 elements in Square and then would stay at 25.

    let interestingNumbers = [
        "Prime": [2, 3, 5, 7, 11, 13],
        "Fibonacci": [1, 1, 2, 3, 5, 8],
        "Square": [1, 4, 9, 16, 25]
    ]
    var largest = 0
    for (kind, numbers) in interestingNumbers {
        println("kind: \(kind)")
        for number in numbers {
            if number > largest {
                largest = number
            }
        }
    }
    largest
    

    This prints:

    kind: Square
    kind: Prime
    kind: Fibonacci
    
    0 讨论(0)
  • 2020-11-27 12:35

    This is a user-defined function to iterate through a dictionary:

    func findDic(dict: [String: String]){
        for (key, value) in dict{
        print("\(key) : \(value)")
      }
    }
    
    findDic(dict: ["Animal":"Lion", "Bird":"Sparrow"])
    //prints Animal : Lion 
             Bird : Sparrow
    
    0 讨论(0)
  • 2020-11-27 12:36

    If you want to iterate over all the values:

    dict.values.forEach { value in
        // print(value)
    }
    
    0 讨论(0)
  • 2020-11-27 12:40

    Arrays are ordered collections but dictionaries and sets are unordered collections. Thus you can't predict the order of iteration in a dictionary or a set.

    Read this article to know more about Collection Types: Swift Programming Language

    0 讨论(0)
  • 2020-11-27 12:47

    Here is an alternative for that experiment (Swift 3.0). This tells you exactly which kind of number was the largest.

    let interestingNumbers = [
    "Prime": [2, 3, 5, 7, 11, 13],
    "Fibonacci": [1, 1, 2, 3, 5, 8],
    "Square": [1, 4, 9, 16, 25],
    ]
    
    var largest = 0
    var whichKind: String? = nil
    
    for (kind, numbers) in interestingNumbers {
        for number in numbers {
        if number > largest {
            whichKind = kind
            largest = number
        }
      }
    }
    
    print(whichKind)
    print(largest)
    
    OUTPUT:
    Optional("Square")
    25
    
    0 讨论(0)
  • 2020-11-27 12:47

    You can also use values.makeIterator() to iterate over dict values, like this:

    for sb in sbItems.values.makeIterator(){
      // do something with your sb item..
      print(sb)
    }
    

    You can also do the iteration like this, in a more swifty style:

    sbItems.values.makeIterator().forEach{
      // $0 is your dict value..
      print($0) 
    }
    

    sbItems is dict of type [String : NSManagedObject]

    0 讨论(0)
提交回复
热议问题