简体   繁体   中英

Latest version of swift: [String, String?] is not convertible to Dictionary<String, AnyObject>

I updated my xcode yesterday to the new version. I was asked if i wanted to update all my files to the latest version of swift. regrettably i clicked yes and am now working through a mountain of fixes. I'm struggling with swifts new issue with dictionaries, I had a method that took Dictionary? as a parameter which ive now have to change to Dictionary as all sorts of dictionaries get passed into this method. Problem is im now told on some of my dictionaries like the one below that they cannot be converted, how do i change this to Dictionary? I've tried the obvious as! Dictionary at the end but it simple wont let it convert...

let params:Dictionary<String, AnyObject>? = ["Username" : userName!.text, "Password" : "" + password!

The reason is that the text property of UILabel is an optional, so you have to unwrap it

let params : Dictionary<String, AnyObject> = ["Username" : userName!.text!, "Password" : "" + password!.text!]

but why do you declare an obvious non-optional literal dictionary as optional?

由于这些值是可选的,因此必须像下面这样声明:

let params:Dictionary<String, AnyObject?>? = ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM