How to access a random element in a dictionary in Swift

后端 未结 3 1401
深忆病人
深忆病人 2021-02-13 14:30

I got the following code:

var myDic = [String: Customer]()
let s1 = Customer(\"nameee\", \"email1\")
let s2 = Customer(\"nameee2\", \"email2\")
let s3 = Customer         


        
3条回答
  •  旧时难觅i
    2021-02-13 15:19

    Swift 5+ (Xcode 12+) introduces randomise Dictionary key also

    let randomVal = myDict.randomElement()?.key
    

提交回复
热议问题