Swift: Nil is incompatible with return type String
Asked Answered
Z

3

13

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

Zolazoldi answered 13/11, 2015 at 6:19 Comment(4)
you can't return the Nil value in swift, change your return type String in your methodHomogeneity
this may help you #32232892Mukund
show more code of your function, namely its return type.Morbific
@Homogeneity you can return nil in swift when the return type is optional (the Optional type implements NilLiteralConvertible). The issue is that the return type of the method is String. If he wants the ability to return nil then the return type should be String?Spine
D
21

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.

Dessiatine answered 4/8, 2018 at 14:50 Comment(2)
Very nice answer.Brezhnev
Thanks! Much appreciated.Monkhood
L
28

Does your function declare an optional return type?

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

See more on: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html

NOTE

The concept of optionals doesn’t exist in C or Objective-C. The nearest thing in Objective-C is the ability to return nil from a method that would otherwise return an object, with nil meaning “the absence of a valid object.”

Ladida answered 13/11, 2015 at 6:31 Comment(0)
D
21

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.

Dessiatine answered 4/8, 2018 at 14:50 Comment(2)
Very nice answer.Brezhnev
Thanks! Much appreciated.Monkhood
U
0

*Does your function declare an optional return type?

func minAndmax(array:[Int])->(min:Int, max:Int)? {
    if array.isEmpty {
        return nil
    }

    var currentMin = array[0]
    var currentMax = array[0]

    for value in array {
        if value < currentMin {
            currentMin = value
        }
        else if value > currentMax {
            currentMax = value
        }
    
    }
    return (currentMin, currentMax)
}

if let bounds = minAndmax(array:  [8, -6, 2, 109, 3, 71]) {
    print(bounds)
}
Unmixed answered 20/10, 2020 at 11:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.