Swift: Nil incompatible with String return type

I have this code in Swift:

guard let user = username else{ return nil } 

But I get the following errors:

 Nil is incompatible with return type String 

Do any of you know why and how I return zero in this case?

I will be very grateful for your help.

+5
source share
1 answer

Does your function declare an optional return type?

func foo () -> String? {...

More details: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html

Note

The concept of optionals does not exist in C or Objective-C. the closest thing in Objective-C is the ability to return zero from a method that would otherwise return an object, with nil meaning "no valid object."

+18
source

Source: https://habr.com/ru/post/1235859/


All Articles