5

I have a model class which contain these properties:-

class ItemModel:NSObject{
   var itemName:String?
   var itemPrice:String?
   var itemCategory:String?
   var isAvailable:Bool?
}

I have an array of type ItemModel Class

var itemArray = [ItemModel]()

I want to filter this array based on itemCategory

1
  • 1
    Why are all your model's properties optional? Does it make sense to have an item without a name, price, category or availability? Also do you really need to inherit from NSObject? Commented Jul 12, 2017 at 15:39

4 Answers 4

6

try this

let filteredArray = self.originalArray.filter({($0.itemCategory.localizedCaseInsensitiveContains(searchText))!})
Sign up to request clarification or add additional context in comments.

1 Comment

thanks its working
6

You can try something like:

itemArray.filter({$0.itemCategory == "Test"})

$0 will present the object in the array and you can use it for every property in your object.

Comments

0

Use filter to do that:

itemArray.filter({$0.itemCategory == "abc"})

Comments

0

Can you try

self.yourArrayList.filter({$0.itemName == xyz}) like this

Inside {} you need to put your condition for filtering

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.