Why print() is printing my String as an optional?

后端 未结 4 1874
醉酒成梦
醉酒成梦 2021-01-23 02:41

I have a dictionary and I want to use some of its values as a key for another dictionary:

let key: String = String(dictionary[\"anotherKey\"])

4条回答
  •  囚心锁ツ
    2021-01-23 03:35

    You can also avoid force unwrapping by using default for the case that there is no such key in dictionary

    var dictionary = ["anotherkey" : 42]
    
    let key: String = 
    String(dictionary["anotherkey", default: 0])
    print(key)
    

提交回复
热议问题