StoreSearch: Categorize SearchResults by category for TableView

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 :wink:

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.

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:

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:

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

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. :slight_smile:

1 Like

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?

The variable i is an index into the search results array but you’re using it to index the categories array. That seems wrong. :wink: 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.

1 Like

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