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

ZbadhabitZ

I have been skimming the StackOverflow questions trying to figure out where I'm going wrong with my code, but I just can't seem to! I am trying to convert my Swift 1.2 project to Swift 2.0, and am having an issue with my class that downloads JSON data.

I am continually receiving the error Unexpected non-void return value in void function.

Here is the code, somewhat truncated, that I am using;

...

class func fetchMinionData() -> [Minion] {

    var myURL = "http://myurl/test.json"

    let dataURL = NSURL(string: myURL)

    let request = NSURLRequest(URL: dataURL!, cachePolicy: .ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 5.0)

    let session = NSURLSession.sharedSession()

    session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
        let minionJSON = JSON(data!)

        var minions = [Minion]()

        for (_, minionDictionary) in minionJSON {
            minions.append(Minion(minionDetails: minionDictionary))
        }

        return minions
        //THIS IS WHERE THE ERROR OCCURS

    }).resume()
}

...

Perhaps I am overlooking something simple, but I'm unsure why my function would be considered void at all. Any thoughts would be immensely appreciated! Thank you!

Andriy Gordiychuk

You have a problem because your line:

return minions

does not return from your function. Instead, it returns from the completion handler in dataTaskWithRequest. And it shouldn't be doing so because that closure is a void function.

The problem which you have results from the fact that dataTaskWithRequest is an asynchronous operation. Which means that it can return later after executing your function.

So, you need to change your design pattern.

One way of doing that would be the following:

static var minions:[Minion] = [] {
    didSet {
        NSNotificationCenter.defaultCenter().postNotificationName("minionsFetched", object: nil)
   }
}



class func fetchMinionData() {

    var myURL = "http://myurl/test.json"
    let dataURL = NSURL(string: myURL)
    let request = NSURLRequest(URL: dataURL!, cachePolicy: .ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 5.0)

    let session = NSURLSession.sharedSession()

    session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
        let minionJSON = JSON(data!)

        var minions = [Minion]()

        for (_, minionDictionary) in minionJSON {
            minions.append(Minion(minionDetails: minionDictionary))
        }

        self.minions = minions
        //THIS IS WHERE THE ERROR OCCURS

    }).resume()
}

Then before calling your function you should register to listen for NSNotification with name "minionsFetched". And only after you get that notification you should process the minions as if they were fetched.

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 3)

Swift NumberOfRowsInSection Unexpected non-void return value in void function

unexpected non void return value in void function

Unexpected non-void return value in void function

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

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)

why in this code would I be getting "Unexpected non-void return value in void function"

Retrieve product localised price - Unexpected non-void return value in void function

Why does Unexpected non-void return value in void function happen?

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

Return value void * in a function

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

Restrict function return value to void

Swift 3: Cannot call value of non-function type '(() -> Void)?'

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

What is the return value if we dont return anything from a non void return typed function in c++?[Experimental]

What's the return value of a non-void function missing return statement?

Warning 'return' with no value, in function returning non-void - What should it return?

void function pointer return value in C

Unclear about return value of a void function in C

Should i return a value for a Future<void> Function?

Why does a void function return a value?