func getUserById(userId: Int) -> User {
if (userId != nil) {
...
}
return nil
}
The above code reported an error: "'nil' is incompatible with return type 'User'", indicating that "nil" is incompatible with the return type "User".
solution:
Change the return value type to Optional type User?
func getUserById(userId: Int) -> User? {
if (userId != nil) {
...
}
return nil
}
In the same way, if you call getUserById(nil), an error will be reported: "'nil' is not compatible with expected argument type 'Int'"
Just change the parameter type to Int?
func getUserById(userId: Int?) -> User? {
if (userId != nil) {
...
}
return nil
}