1

I am making an API call for muscles relating to an exercise, the call looks like this:

func loadPrimaryMuscleGroups(primaryMuscleIDs: [Int]) {
    print(primaryMuscleIDs)
    let url = "https://wger.de/api/v2/muscle"
    Alamofire.request(url).responseJSON { response in
        let jsonData = JSON(response.result.value!)
        if let resData = jsonData["results"].arrayObject {
            let resData1 = resData as! [[String:AnyObject]]
            if resData1.count == 0 {
                print("no primary muscle groups")
                self.musclesLabel.isHidden = true
            } else {
                print("primary muscles used for this exercise are")
                print(resData)
                self.getMuscleData(muscleUrl: resData1[0]["name"] as! String)
            }
        }
    }
}

This returns me a whole list of all the muscles available, I need it to just return the muscles the exercise requires. This is presented in exercises as an array of muscle id's, which I feed in via the viewDidLoad below

self.loadPrimaryMuscleGroups(primaryMuscleIDs: (exercise?.muscles)!)

So I am feeding the exercises muscle array into the func as an [Int] but at this point im stumped on how to filter the request so that the resulting muscle data are only the ones needed for the exercise.

I was thinking it would be something like using primaryMuscleIDs to filter the id property of a muscle in the jsonData response, but I'm not sure how to go about that?

Hopefully, I have explained it clearly enough to come across well.

2
  • Is the JSON() object with the arrayObject property something you've implemented? Commented Jan 10, 2017 at 20:12
  • its from swiftyJSON i believe Commented Jan 10, 2017 at 21:19

1 Answer 1

1

You'd want to do something like this in your else block:

var filteredArray = resData1.filter { item in
    //I'm not exactly sure of the structure of your json object,
    //but you'll need to get the id of the item as an Int
    if let itemId = item["id"] as? Int  {  
        return primaryMuscleIDs.contains(itemId)
    }
    return false
}

//Here with the filtered array

And since the Alamofire request is asynchronous, you won't be able to return a result synchronously. Your method will need to take a callback that gets executed in the Alamofire response callback with the filtered response.

Something like (but hopefully with something more descriptive than an array of Any:

func loadPrimaryMuscleGroups(primaryMuscleIDs: [Int], callback: ([Any]) -> ()) {
    //...
    Alamofire.request(url).responseJSON { response in
        //...
        //once you get the filtered response:
        callback(filteredArray)
    }
}

Finally, check out How to parse JSON response from Alamofire API in Swift? for the proper way to handle a JSON response from Alamofire.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the comment im just attempting to integrate it, you can find the muscle JSON object here if it helps the answer wger.de/api/v2/muscle I have a separate get function for alamofire I didnt include in the question as this is just about filtering at the moment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.