Reputation: 19612
I have 3 categories:
Each category can have a number of different name brands eg.
1.Sneakers > Nikes, Adidas, Reeboks
2.Pants > Lees, Levi's
3.Shirts > Ralph Lauren, Abercrombie
My users pick from a category and then they can add as many name brands as they want to it. After they've chosen the category and put whatever brand names they want, I need 2 tableViews (or collectionViews). The first tableView should show the category they picked and the 2nd tableview should show the brands they named in that category.
My problem is sorting by category. I know how to display all of the names they choose, but not the categories leading to the names.
The issue I'm having is if a user choose the Shirts category twice, the tableView to display it displays the shirt category twice, when it should only display it once.
I found this from Apple but it doesn't show the examples in Swift.
Any similar StackOverflow q/a's in Swift are fine as an answer.
Upvotes: 1
Views: 813
Reputation: 4163
If your array contains duplicate in category.
I hope it must be containing duplicate, as you can see the duplicate in table view, so use the NSOrderedSet
to remove duplicate.
Here is the demo code demonstrating use of NSOrderedSet
Objective C Version:
NSArray *category = [NSArray arrayWithObjects:@"Sneakers", @"Pants", @"Shirts",@"Pants", @"Shirts", nil];
NSLog(@"Before using ordered set : %@", category.description);
// iOS 5.0 and later
NSArray * newCategoryArray = [[NSOrderedSet orderedSetWithArray:category] array];
NSLog(@"After using ordered set : %@", newCategoryArray.description);
Swift Version :
let category = ["Sneakers", "Pants", "Shirts","Pants", "Shirts"];
NSLog("Before using ordered set : %@", category.description);
let newCategoryArray = NSOrderedSet(array: category).array;
NSLog("After using ordered set : %@", newCategoryArray.description);
Output :
Before using ordered set : (
Sneakers,
Pants,
Shirts,
Pants,
Shirts
)
After using ordered set : (
Sneakers,
Pants,
Shirts
)
Happy coding ...
Upvotes: 1