-1

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

                }

            }
Slygoth
  • 333
  • 6
  • 17

1 Answers1

0

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)
}
FredLoh
  • 1,804
  • 18
  • 27
  • Nope it didnt. For some reason – Slygoth Dec 05 '15 at 02:14
  • I've edited my code so it also returns something if an error is found. Code runs fine on my end so please provide some context as to how it is failing. Also consider removing Id: String since you seem to do nothing with it. – FredLoh Dec 05 '15 at 02:18
  • Ok that work. Another problem I’m having now i used the data. But another thing I’m running giving the same problem where it excites before its completed – Slygoth Dec 05 '15 at 02:24
  • I sure will thanks much for the help. See the edited post for other problem – Slygoth Dec 05 '15 at 02:28
  • Please post the new question as a separate question so future users can search for answers to their problems more clearly. Also if you repost the code please provide more context. For example if you are calling a function (say ```displayCatName()```) please post the function definition. – FredLoh Dec 05 '15 at 02:30
  • I cant post a new question for the next 90 minutes – Slygoth Dec 05 '15 at 02:37
  • Edit this question and add more code then. It's very unclear from what you posted. – FredLoh Dec 05 '15 at 02:38
  • Can you provide the code, rather than saying above. – FredLoh Dec 05 '15 at 03:07
  • However you are populating your information put a completion handler on it, then run ```vc.displayCatName() ``` once it completes. Like I said it is difficult if you do not post more code. – FredLoh Dec 05 '15 at 17:51