Why does implicitly unwrapped optional not unwrap in dictionary of type [String : Any]

后端 未结 4 714
灰色年华
灰色年华 2020-12-06 15:09

If I have an implicitly unwrapped optional declared in my class which I then reference in a Dictionary of type [String : Any], it doesn\'t get unwr

4条回答
  •  没有蜡笔的小新
    2020-12-06 15:16

    When you define a dictionary of type

    let dictionary = [String:Any]()
    

    you can put anything in this dictionary

    like

     dictionary["name"] = "xyz"
    

    and

     dictionary["code"] = 123
    

    while in

    let dictionary = [String:String]()
    

    you can only put an only string value

    that's why you have to unwrap value when you are accessing the value because it can be anyting

提交回复
热议问题