0

I have a RegistrationViewController where I want users to register their accounts. I've also created a server interaction class called ServerInteraction.

Here is how I call a function from my ServerInteraction to register new user:

var json: JSON = ServerInteraction.registerUser(self.fullnameTextField.text, phone: self.telephoneTextField.text, password: self.passwordTextField.text, photo: self.image)

And here is the declaration of this function in ServerInteraction class:

class func registerUser(fullname: String, phone: String, password: String, photo: UIImage?) -> JSON {
    // set request params
    var params = [
        C.register.rawValue: "true", C.registerName.rawValue: fullname, C.password.rawValue: password, C.phone.rawValue: phone, C.registerDescription.rawValue: "", C.registerType.rawValue: "Student"
    ]

    if let picture = photo {
        let imageData = UIImageJPEGRepresentation(picture, 0.2)
        let urlRequest = urlRequestWithComponents("http://host.com/addUser.php", parameters: params, imageData: imageData!)

        Alamofire.upload(urlRequest.0, data: urlRequest.1)
            .progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
                println("\(totalBytesWritten) / \(totalBytesExpectedToWrite)")
            }
            .responseJSON { (_, _, data, error) in
                if error == nil {
                    // check for coming data
                    if let data: AnyObject = data {
                        // convert to json
                        let json = JSON(data)
                        dispatch_async(dispatch_get_main_queue(), {
                            return json
                        })
                    }
                } else {
                    dispatch_async(dispatch_get_main_queue(), {
                        return JSON(["error": true]) 
                    })
                }
            }
    }
}

I want to send JSON result from server back to RegistrationViewController and use it there. But the compiler says:

Missing return in a function expected to return 'JSON'

at the end of the function.

Then I've tried to create a variable to store JSON result and then return it but it is always nil because server answers a bit later and it returned nil value.

class func registerUser(fullname: String, phone: String, password: String, photo: UIImage?) -> JSON {
    // set request params
    var params = [
        C.register.rawValue: "true", C.registerName.rawValue: fullname, C.password.rawValue: password, C.phone.rawValue: phone, C.registerDescription.rawValue: "", C.registerType.rawValue: "Student"
    ]
    // added a variable here
    var result: JSON!
    if let picture = photo {
        let imageData = UIImageJPEGRepresentation(picture, 0.2)
        let urlRequest = urlRequestWithComponents("http://host.com/addUser.php", parameters: params, imageData: imageData!)

        Alamofire.upload(urlRequest.0, data: urlRequest.1)
            .progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
                println("\(totalBytesWritten) / \(totalBytesExpectedToWrite)")
            }
            .responseJSON { (_, _, data, error) in
                if error == nil {
                    // check for coming data
                    if let data: AnyObject = data {
                        // convert to json and set to result variable
                        result = JSON(data)
                    }
                } else {
                    // set an error to result variable
                    result = JSON(["error": true]) 
                }
            }
    }
    // nil value here
    return result 
}

How can I wait for the result here or is there any other solutions to solve this issue?

mkz
  • 2,302
  • 2
  • 30
  • 43
  • 6
    This has been asked a zillion times... See this [answer](http://stackoverflow.com/questions/30771881/returning-a-value-from-a-function-with-alamofire-and-swiftyjson?rq=1) for example, but there's a lot of other answers you could use. Keywords: *asynchronous* and *completion handler*. – Eric Aya Sep 16 '15 at 12:58
  • Thank you @EricD., this is what I was looking for! – mkz Sep 16 '15 at 13:00

0 Answers0