Return value for method from closure in Swift
Asked Answered
H

4

6

Is there a way that I can return value for shouldPerformSegueWithIdentifier method from my closure? Or should I do it differently?

override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {

    getDataFromSomewhere() { succeeded, data in
        if succeeded {
            // Should return true here            
        } else {
            self.errorAlert("Error", message: "Can't get data...")
            // Should return false here
        }
    }

}
Herr answered 9/6, 2015 at 16:52 Comment(0)
C
6

You can't make shouldPerformSegueWithIdentifier:sender: return from the point where you put // Should return true here. You need to do something like this instead:

override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {
    var returnValue: Bool = false

    getDataFromSomewhere() { succeeded, data in
        if succeeded {
            returnValue = true
        } else {
            self.errorAlert("Error", message: "Can't get data...")
            returnValue = false
        }
    }

    return returnValue
}

Note that this will only work if getDataFromSomewhere executes the closure before getDataFromSomewhere returns. If getDataFromSomewhere stores the closure, and arranges for it to be called later on (after an asynchronous network request, for example), then this won't work. It will just make shouldPerformSegueWithIdentifier:sender: return the value you used to initialize returnValue.

Constructionist answered 9/6, 2015 at 17:33 Comment(0)
O
0

You can convert this method to sync by using dispatch group mechanism.

  let dispatchGroup = DispatchGroup()

      override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {
             var returnValue: Bool = false
            dispatchGroup.enter()
            
            DispatchQueue.global().async {
                getDataFromSomewhere() { succeeded, data in
                    if succeeded {
                       returnValue = true     
                    } else {
                        self.errorAlert("Error", message: "Can't get data...")
                        returnValue = false
                    }
                    self.dispatchGroup.leave() 
                }
            }
            
            dispatchGroup.wait()
    
            return returnValue
            }
Other answered 6/10, 2022 at 8:45 Comment(0)
L
-1

if your getDataFromSomewhere closure is async after then you have no other solution than using completion handlers.

override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?, , completion: @escaping ((Bool) -> Void)) {
    getDataFromSomewhere() { succeeded, data in
        if succeeded {
            completion(true)
        } else {
            self.errorAlert("Error", message: "Can't get data...")
            completion(false)
        }
    }
}
Loleta answered 16/9, 2021 at 9:28 Comment(2)
shouldPerformSegueWithIdentifier is a an override, so changing its signature will prevent the system calling it.Hereditament
ah okay I didn't notice that, thank you.Loleta
M
-2

Are you opposed to using prepareforsegue and an unwind segue?

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "fuelButtonUnhide"{

        if let vc = segue.destinationViewController as? DateViewController {

            vc.fuelHidden = false
        }
    } 
}

@IBAction func unwindDateViewToAddFuel(segue: UIStoryboardSegue){
    //Change any data from the date view here
    println("Hit done from date view")
}

You can pass data in the prepareforsegue and retrieve data in the unwind action.

Marucci answered 9/6, 2015 at 17:21 Comment(1)
That's not the case. I need to know can I return "methods return" from closure.Herr

© 2022 - 2024 — McMap. All rights reserved.