Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nil is not compatible with expected argument type 'String'

Tags:

ios

swift

swift3

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.

like image 750
Derivative Avatar asked Jan 15 '17 06:01

Derivative


1 Answers

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.

like image 69
Benjamin Lowry Avatar answered Sep 19 '22 03:09

Benjamin Lowry