Swift: Nil is incompatible with return type String

user2924482

I have this code in Swift:

guard let user = username else{
        return nil
    }

But I'm getting the following errors:

Nil is incompatible with return type String

Any of you knows why or how I return nil in this case?

I'll really appreciate your help

Josue Gisber

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Convert String to NSURL is return nil in swift

From Dev

how do you return nil if a certain type is passed to a function in Swift?

From Java

The type of getMethod() from the type MyClass is String, this is incompatible with the descriptor's return type: U

From Dev

Incompatible return type within customList

From Dev

Swift Delegate return nil

From Dev

swift sequenceType return nil

From Dev

Swift Delegate return nil

From Dev

Return enum type based on string value in swift?

From Dev

swift code return alert type String not working

From Dev

Swift | assigning nil to string

From Dev

Compare to nil and return a bool in Swift

From Dev

'queryForTable' cannot return 'nil' In Swift

From Dev

iOS: dateformatter in swift return nil

From Dev

'queryForTable' cannot return 'nil' In Swift

From Dev

Wildcard for Set in Map - return type incompatible - Why?

From Dev

The return type is incompatible with FragmentPagerAdapter.getItem(int)

From Dev

grails error: return type of getGrailsApplication() is incompatible

From Dev

The return type is incompatible with <Interface.functionName>

From Dev

Incompatible types: bad return type in lambda expression?

From Dev

"Attempting to use an incompatible return type" with Interface Inheritance

From Dev

incompatible return type from struct function - C

From Dev

Incompatible types in generic return type method (Java)

From Dev

The return type is incompatible with FragmentPagerAdapter.getItem(int)

From Dev

The return type is incompatible with <Interface.functionName>

From Dev

ArgumentException: method return type is incompatible (Mono)

From Java

In Swift, Array [String] slicing return type doesn't seem to be [String]

From Java

How to Return Nil String in Go?

From Dev

Converting String to URL return nil

From Dev

Swift: overriding method with selector has incompatible type

Related Related

  1. 1

    Convert String to NSURL is return nil in swift

  2. 2

    how do you return nil if a certain type is passed to a function in Swift?

  3. 3

    The type of getMethod() from the type MyClass is String, this is incompatible with the descriptor's return type: U

  4. 4

    Incompatible return type within customList

  5. 5

    Swift Delegate return nil

  6. 6

    swift sequenceType return nil

  7. 7

    Swift Delegate return nil

  8. 8

    Return enum type based on string value in swift?

  9. 9

    swift code return alert type String not working

  10. 10

    Swift | assigning nil to string

  11. 11

    Compare to nil and return a bool in Swift

  12. 12

    'queryForTable' cannot return 'nil' In Swift

  13. 13

    iOS: dateformatter in swift return nil

  14. 14

    'queryForTable' cannot return 'nil' In Swift

  15. 15

    Wildcard for Set in Map - return type incompatible - Why?

  16. 16

    The return type is incompatible with FragmentPagerAdapter.getItem(int)

  17. 17

    grails error: return type of getGrailsApplication() is incompatible

  18. 18

    The return type is incompatible with <Interface.functionName>

  19. 19

    Incompatible types: bad return type in lambda expression?

  20. 20

    "Attempting to use an incompatible return type" with Interface Inheritance

  21. 21

    incompatible return type from struct function - C

  22. 22

    Incompatible types in generic return type method (Java)

  23. 23

    The return type is incompatible with FragmentPagerAdapter.getItem(int)

  24. 24

    The return type is incompatible with <Interface.functionName>

  25. 25

    ArgumentException: method return type is incompatible (Mono)

  26. 26

    In Swift, Array [String] slicing return type doesn't seem to be [String]

  27. 27

    How to Return Nil String in Go?

  28. 28

    Converting String to URL return nil

  29. 29

    Swift: overriding method with selector has incompatible type

HotTag

Archive