# StoreSearch: Categorize SearchResults by category for TableView

**URL:** <https://forums.kodeco.com/t/storesearch-categorize-searchresults-by-category-for-tableview/11686>\
**Category:** iOS Apprentice\
**Created:** [March 31, 2016, 7:11pm UTC](https://forums.kodeco.com/t/storesearch-categorize-searchresults-by-category-for-tableview/11686 "2016-03-31T19:11:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![daxrahusen](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/daxrahusen/32/110937_2.png) [@daxrahusen](https://forums.kodeco.com/u/daxrahusen)\
**Post date:** [March 31, 2016, 7:11pm UTC](https://forums.kodeco.com/t/storesearch-categorize-searchresults-by-category-for-tableview/11686/1 "2016-03-31T19:11:58Z")

</div>

Dear all,

I’m busy with filtering the (app) SearchResults by genre.  
The end result is to populate a `TableView` with Section by genre.

To achieve this I’m making my own simper struct object (for now) to populate it with the following variables:

> struct CategoryResult {  
> var sectionName : String!  
> var sectionObjects : [SearchResult]  
> }

After downloading and parsing it from the web I’m doing the following to fill an categoryResult Array:

> private func parseCategoryName(searchResults: [SearchResult]) {
> 
> ```
> print(searchResults.count)
>     
> objectsArray = [CategoryResult]()
> 
> ```

> ```
> for (i, review) in searchResults.enumerate() {
>         
> print(i)
> print("NAME: " + review.name + " & GENRE: " + review.genre)
>         
> if objectsArray.isEmpty {
>         
> let firstCategory = CategoryResult(sectionName: review.genre, sectionObjects: [review])
> objectsArray.append(firstCategory)
>         
> } else {
>             
> for (categoryIndex, object) in objectsArray.enumerate() {
>                 
> if object.sectionName == review.genre {
>                     
> objectsArray[categoryIndex].sectionObjects.append(review)
>                     
> return
>                     
> } else {
>                     
> let newCategory = CategoryResult(sectionName: review.genre, sectionObjects: [review])
> objectsArray.append(newCategory)
>                     
> return
> }
> }
> }
> }
> }
> 
> ```

This all works fine except It will only populate the objectsArray with 2 CategoryResults.  
This is not the right amount. When printing the searchResults parameter in the function it show far more than that.  
In debug mode it looks like after 2 for loops it will end and reload the `TableView` .

Anyone has an idea how this happens.

Many thanks in advance.

Dax

BTW: Love your Apprentice Book!  
Go Holland 😉

 ![](https://raywenderlich-com-v3-forums.s3.amazonaws.com/original/2X/4/4f5ddbbe3ae8dc3a0c98cf305b06602d6d8ab1a4.png)

---

<div class="post-metadata">

**Author:** ![hollance](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/hollance/32/109009_2.png) [@hollance](https://forums.kodeco.com/u/hollance)\
**Post date:** [April 1, 2016, 8:08am UTC](https://forums.kodeco.com/t/storesearch-categorize-searchresults-by-category-for-tableview/11686/2 "2016-04-01T08:08:21Z")

</div>

Why do you have a `return` statement in there? That will immediately exit the current method, which is why you only get 2 things in the array.

---

<div class="post-metadata">

**Author:** ![daxrahusen](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/daxrahusen/32/110937_2.png) [@daxrahusen](https://forums.kodeco.com/u/daxrahusen)\
**Post date:** [April 1, 2016, 11:50am UTC](https://forums.kodeco.com/t/storesearch-categorize-searchresults-by-category-for-tableview/11686/3 "2016-04-01T11:50:06Z")

</div>

I thought it only moved out of the for loop, sorry for that.  
Also forgot to mention that if I do remove these return statements it will add way to much categories in the categories array:

 ![](https://raywenderlich-com-v3-forums.s3.amazonaws.com/original/2X/2/2114bed542261f9e35be91a09222d21b819b70bc.png)

But when I use an `break` instead of an return it will do the right amount of apps but not categorize them if the have te same genre:

 ![](https://raywenderlich-com-v3-forums.s3.amazonaws.com/original/2X/6/6e00a5f441646ee9a5465a7df44484e9e5771881.png)

There must be something wrong with the loops, but can’t seem to find out why…  
So close!

---

<div class="post-metadata">

**Author:** ![hollance](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/hollance/32/109009_2.png) [@hollance](https://forums.kodeco.com/u/hollance)\
**Post date:** [April 1, 2016, 12:07pm UTC](https://forums.kodeco.com/t/storesearch-categorize-searchresults-by-category-for-tableview/11686/4 "2016-04-01T12:07:40Z")

</div>

In “pseudo code” I’d do it like this:

```
loop through all search results

    if not yet have a category for this search result
        add a new category

    add the search result to this category

```

And then I would put the code that determines if there is a category for the search result into its own method. This should return the `CategoryResult` object if found or `nil` if not found.

I hope this helps. 🙂

---

<div class="post-metadata">

**Author:** ![daxrahusen](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/daxrahusen/32/110937_2.png) [@daxrahusen](https://forums.kodeco.com/u/daxrahusen)\
**Post date:** [April 1, 2016, 10:15pm UTC](https://forums.kodeco.com/t/storesearch-categorize-searchresults-by-category-for-tableview/11686/5 "2016-04-01T22:15:48Z")

</div>

I understand what you mean with the pseudo code, though it still does not give me the right Result.

What I now have is this:

> private func parseCategoryName(searchResults: [SearchResults]) → [CategoryResult]{
> 
> ```
> var categories = [CategoryResult]()
>     
> for (var i, result) in searchResults.enumerate() {
>         
> print(result.name)
>         
> if categories.isEmpty {
>             
> let category = CategoryResult(sectionName: review.genre, sectionObjects: [result])
> categories.append(category)
>             
> } else {
>             
> if result.genre == categories[i].sectionName {
>                 
> categories[i].addReview(result)
>                 
> } else {
>                 
> let category = CategoryResult(sectionName: result.genre, sectionObjects: [result])
> categories.append(category)
> }
>             
> }
>         
> }
>     
> return categories
> }
> 
> ```

I saw that in the previous code it will never execute the compare if ‘result.genre’ statement. I think this will be a better way. It only gives me the wrong index after 2 loops.  
Error is: fatal error: Index out of range.  
Do you have an solution for this?

---

<div class="post-metadata">

**Author:** ![hollance](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/hollance/32/109009_2.png) [@hollance](https://forums.kodeco.com/u/hollance)\
**Post date:** [April 2, 2016, 9:59am UTC](https://forums.kodeco.com/t/storesearch-categorize-searchresults-by-category-for-tableview/11686/6 "2016-04-02T09:59:40Z")

</div>

The variable `i` is an index into the search results array but you’re using it to index the categories array. That seems wrong. 😉 If `i` is 3 and the categories array only has 1 thing in it (for example), the you get an index out of range error.

---

<div class="post-metadata">

**Author:** ![daxrahusen](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/daxrahusen/32/110937_2.png) [@daxrahusen](https://forums.kodeco.com/u/daxrahusen)\
**Post date:** [April 2, 2016, 10:55am UTC](https://forums.kodeco.com/t/storesearch-categorize-searchresults-by-category-for-tableview/11686/7 "2016-04-02T10:55:28Z")

</div>

Thank you very much for you insight! 🙂  
If found out that I need to increment the amount of [i] with 1, when an categorie genre already exist.
