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

后端 未结 4 696
灰色年华
灰色年华 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:22

    • Any can represent an instance of any type at all, including function types and optional types.
    • AnyObject can represent an instance of any class type.

    And your aString is an instance object which should not be use to declare in property Area, if you put that line in any function/instance function()then it will work perfectly, because it will be your instance type then.

    In One word, you can not declare instance type in property area in Swift.

    override func viewDidLoad() {
       let params : [String : Any] = ["myString": aString]
    }
    

    Even in Property Area, You can not do like following:

    var aString: String! = "String"
    var abc = aString
    

    You have to abc = aString do in any Method() to get work.

    Hope it will help you.

提交回复
热议问题