Iterating Through a Dictionary in Swift

后端 未结 7 726
醉话见心
醉话见心 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: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]

提交回复
热议问题