简体   繁体   中英

Nil is not compatible with expected argument type 'String'

I am currently converting my swift 2.3 code to swift 3 and I am getting the above error on the following line:

setSharedPassword(nil, account: account, completion: completion)

Would the appropriate solution be just to replace nil with ""? The error goes away when I do it. I am looking for an explanation. Please help.

If your setSharedPassword func's first parameter is of type String then you will not be able to set this as nil because it is not optional. If you want to be able to set it as nil, then you could do something like this for your func:

func setSharedPassword(string: String?, account: ...)

The reason why "" works is because it is still a value for a String, just a value that has no characters.

Of course this answer is assuming this is your own func. If setSharedPassword is not yours, then you either need to come up with a String that represents no password, or just supply "" as before.

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