How to return nil object in non void function - swift 2.1?

Pawan Joshi

Here is my code which is giving me an error

class Table_Users: NSManagedObject {

    class func getUserFromUserID(userID:String)->Table_Users
    {
        var user:Table_Users? = nil

        if let delegate = UIApplication.sharedApplication().delegate as? AppDelegate
        {
            let moc = delegate.managedObjectContext


        }
        else
        {

        }
        return user
    }

}

It says i can't return a nil value in non-void function

what else can i return. or how should i write this function the other way ?

using

return user!

is not an option for me. because i have read that if value of object is nil, it will crash.

rahulchona

Use “?” with function’s return type instead

replace "class func getUserFromUserID(userID:String)->Table_Users" with "class func getUserFromUserID(userID:String)->Table_Users?"

Updated Code:

class Table_Users: NSManagedObject {

    class func getUserFromUserID(userID:String)->Table_Users?
    {
        var user:Table_Users? = nil

        if let delegate = UIApplication.sharedApplication().delegate as? AppDelegate
        {
            let moc = delegate.managedObjectContext


        }
        else
        {

        }
        return user
    }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Unexpected Non-Void Return Value In Void Function (Swift 2.0)

Unexpected non-void return value in void function (Swift 3)

Swift NumberOfRowsInSection Unexpected non-void return value in void function

Return nil in swift function

swift Non-void function should return a value in a guard let

unexpected non void return value in void function

unexpected non-void return value in void function in new Swift class

Unexpected non-void return value in void function - Swift 4 (Firebase, Firestore)

Unexpected non-void return value in void function swift 4 using json serialisation

Unexpected non-void return value in void function Swift3

Unexpected non-void return value in void function(swift3)

Class understanding: Unexpected non-void return value in void function (swift 3)

How to convert 'void *' return from a C function to 'UnsafeMutableRawPointer' in Swift 3?

How to Nil a object in Swift

Is the return command in a non-void function necessary?

Type No return, in function returning non-void

Non-void function potential lack of return

How to return in a function if a variable is nil?

How to have "factory function" return a non copyable object?

Swift iOS Firebase -If the FirebaseAuth.FIRUser Object isn't nil how is it possible for the .email on it to return nil?

Unexpected non-void return value in void function

'Unexpected non-void return value in void function' in swiftui

Swift 2 - UnsafeMutablePointer<Void> to object

How to check object is nil or not in swift?

How to fix "error: control reaches end of non-void function [-Werror=return-type]" at HackerRank?

How to mock a void return method affecting an object

How to handle Core Data custom methods what return an object or nil in Obj C or Swift

Swift function with Void return type versus no return type

How to return the value of function 2 from within function 1

TOP Ranking

HotTag

Archive