Return statement returns before information is received

I am having a little trouble with my swift code. The ending return statement runs before the the JSON value is stored so it keeps giving me nil. How can i do the return after the value been received?

func getArticleInfo(Id: String) -> String {
    let url = val1 + val2 + val3
    Alamofire.request(.GET, url).responseJSON { response in
        switch response.result {
        case .Success:
            if let value = response.result.value {
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
                    let json = JSON(value)
                    let singleAsset = json["url"].string
                }
            }
        case .Failure(let error):
            print(error)
        }
    }
return singleAsset
}

Thanks for the help the other problem I’m having. SEE BELOW I am trying to get the categories to populate with all the information then call the vc.displayCatName() after its done. But it does it late and i have to refresh the page before i can see the information.

Above that is just me assigning the JSON values to the keys that populate categories BELOW. But the vc.displayCatName() is a function from another view controller but it gets run before the category values are populated. So the only way i see the values is if i refresh the page manually using the Pull to Refresh. So i want the information to be populated then vc.displayCatName() should run

                            self.getAsset(id!) { (result) -> Void in
                            print("this is result \(result)")
                            let categories = Categories (categoryName: catName, imageId: id, catIdNumber: catIdNumber, imageUrl: result)
                            vc.cats.append(categories)
                        }
                            }
                    }
                    dispatch_async(dispatch_get_main_queue()) {
                        vc.displayCatName()

                }

            }

Answers


The reason for this is because the call that you are making is asynchronous in nature. Instead consider using a completion handler.

func getArticleInfo(Id: String, success: (String) -> Void ) {
    let url = "www.Test.com"
    Alamofire.request(.GET, url).responseJSON { response in
        switch response.result {
        case .Success:
            if let value = response.result.value {
                    let json = JSON(value)
                    let singleAsset = json["url"].string
                    success(singleAsset!)
            }
        case .Failure(let error):
            print(error)
            success("TEST")
        }
    }
}

To Call:

getArticleInfo("test") { (asset) -> Void in
        print(asset)
}

Need Your Help

Is there a way to get count of number methods used in a jar file

java android dex

Can i have the count of all methods used in a jar file .

Import module to gradle

android gradle android-studio

I've cloned the volley library to /Users/user/volley