0

This is my loop

for category in categories! {
    NearbyPlaces.getNearbyPlaces(by: category?.name ?? "food", coordinates: currentLocation!, radius: radius, token: self.response?.nextPageToken, completion: didReceiveResponse)  
}

where categories is an array with inside a list of categories of places (google places) (like restaurant, gym, bar) and i use it with a lot of other functions to search nearby places of those categories. My problem is this, now as it is now, it all works properly, so if in my array categories i have for example 2 elements: bar and gym and near to me there are 10 restaurants and 10 gyms, he will load in my tableView 10 restaurants and 10 gyms. But now that i added a limit to the loadable places i have a problem, if around me there are 10 restaurants and 10 gyms and the limit is (for example) 4, he will not load to me 2 restaurants and 2 gyms but 4 restaurants or 4 gyms, depends on what comes first in the array, so i would like to know how to modify the cycle so that it always uses a different element of the array (so one time use the element in the index 0, after the element in the index 1 etc.)

EDIT

you are right guys, but is not easy to explain clearly my problem, substantially i have two VC with both a tableView, in the first you can chose multiple categories of places (google places) who'll go to fill the array categories, in the second should load x places in x radius (geolocalization) based on the categories chosen in the previous controller, if i don't add a limit of places that can load in my tableview, the loop for does exactly the iterate through the categories one-by-one the PROBLEM is he does not load a place by category at a time but all places of the first item/category, then all places of the second etc. So if i add a limit for example 4 and i have two categories chosen in my array, he will not load 2 places of the first category and two of the second but if near to me (for example) there are 4 or more places of the type of the first category, he will load 4 places of the first type, so this is my problem

  • Your question is very vague. The for loop does already exactly what you seem to be asking for (iterate through the categories one-by-one). The question seems to be very related / dependent on the GooglePlaces API, but you don't mention that at all. Edit the question to make it more clear. – fishinear Nov 08 '17 at 13:48
  • @fishinear i edited the question –  Nov 08 '17 at 14:19

2 Answers2

0

I would suggest that you generate an array of possible matches for the category and then use shuffle to randomise the order of the results in the array. That way you should get a different set of matches each time (depending on how many actual results you have for the category of course)

How do I shuffle an array in Swift?

Flexicoder
  • 8,251
  • 4
  • 42
  • 56
0

The question has much more to do with how the GooglePlaces API works, then with any for-loop. That said, and without knowing much about the GooglePlaces API, your best bet is probably not to restrict the number of results that GooglePlaces returns. Then sift through all the returned results yourself to select which ones you want to show.

fishinear
  • 6,101
  • 3
  • 36
  • 84