How do you live - filter NSArray or NSMutableArray?

I would like to have an NSArray or NSMutableArray that always shows a filtered representation of the dataset. That is, if the filter is set to "show me things with the letter a" and the "blah" element is added, this element will automatically be displayed in the list. However, if "bluh" was added, it will not appear in the filtered list (but it will still be present in the underlying dataset).

I see that there are filters for NSArray and NSMutableArray, but these are single shot methods. That is, the filter occurs when you call the method and never again (unless, of course, you again called the filter method).

I'm from the Java world, I used Glazed Lists for this kind of thing. I was hoping for a similar solution, baked in Cocoa.

+3
source share
2 answers

Most likely, you will want to use the NSArrayController , as suggested by Ole.

You can use setFilterPredicate:on it, as suggested, and then you want to access the objects by calling arrangedObjectson the controller. You do not need it setAutomaticallyRearrangesObjects:if you are not going to sort your data (using sort descriptors and instances of NSSortDescriptor).

NSArrayController , , , , , . NSSearchField .

- , , - , , NSArrayController.

+4

iPhone, NSArrayController, . , setFilterPredicate: setAutomaticallyRearrangesObjects:YES .

, , Key-Value Observing. , filterUsingPredicate:, , , , evaluateWithObject: , , .

+1

Source: https://habr.com/ru/post/1734886/


All Articles