Swift: Nil is incompatible with return type String
You have to tell the compiler that you want to return nil
. How do you that? By assigning ?
after your object. For instance, take a look at this code:
func newFriend(friendDictionary: [String : String]) -> Friend? {
guard let name = friendDictionary["name"], let age = friendDictionary["age"] else {
return nil
}
let address = friendDictionary["address"]
return Friend(name: name, age: age, address: address)
}
Notice how I needed to tell the compiler that my object Friend
, which I'm returning, is an optional Friend?
. Otherwise it will throw an error.
Return nil in swift function
To fix the error you need to return an Optional: List?
func listForName (name: String) -> List? {
if let list = listsDict[name] {
return list
} else {
return nil
}
}
Or just return listsDict[name]
since it will either be optional or have the list itself.
func listForName (name: String) -> List? {
return listsDict[name]
}
But i don't want to return something like empty List object, i want to return nothing when optional is empty. How to do that?
You have several choices:
- Return optional List (List?)
- Return an empty list when no data is found
- Return an exception (depends on context)
- Use an enum to represent Either/Result (similar to Optional but could be better depending on your use-case)
Can I give back nil instead of a tuple?
Did you try this,
... { q -> (String?, GetUserQuestionsOut?) in
}
or
... { q -> (String, GetUserQuestionsOut)? in
}
on the other hand, maybe you could consider using struct(model) or typealias instead of tuple
nil is not compatible with the expected argument type '[NSObject : AnyObject]'
Only nullable types (such as optionals or types that conform to the protocol NilLiteralConvertible
) can be nil
, or compared to nil
. Therefore, you have following options:
1. Make the parameter optional
convert the parameter of the function to [NSObject : AnyObject]?
2. Pass an empty dictionary
just call:
self.silentPostData(
persist.getObject(
mdmiosagent_Constants.SERVERNAMEKEY) as String,
serverport: persist.getObject(mdmiosagent_Constants.SERVERPORTKEY) as String,
serverurl: mdmiosagent_Constants.NATIVE_APP_SERVLET,
parameters: [:],
urldata: jsonData
)
)
Early return from a function which has a return type of [String] in Swift 3
You can solved this error two ways.
Either change return type to
[String]?
from[String]
means make
return type optional.func fetchPerson() -> [String]? {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return nil
}
.......
.......
}Either Change return statement to
return []
fromreturn nil
means
return empty array.func fetchPerson() -> [String] {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return []
}
.......
.......
}
Nil is not compatible with expected argument type 'String'
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.
Related Topics
Uibezierpath Subclass Initializer
How to Create an Alert in a Swift File Model That Works for Various View Controller
Running an iOS7 Project Under Xcode 6 - Image Assets Don't Show Up
Cgaffinetransform -How to Align Video in Screen Center
Firebase Authentication Error in Other Languages
Checkbox Uitableview with Different Sections
How to Handle Usernotifications Actions in iOS 10
How to Open Amazon App from Within My App
Didoutputsamplebuffer Delegate Not Called
Programmatically Obtaining the Imei or Udid of an iOS Device
Xcode 9.3 - Nspredicate Bool Crash
How to Update iOS 14 Widget Background Color from the App
Nsfilemanager Moveitem Throws Error Code=513
Change Uipopoverview Background + Arrow Color
Delegate Seems to Not Be Working, According to the Console
@Fetchrequest + Case-Insensitive Sorting - Swiftui & Coredata