Вопрос

I've got an array of filepaths and I've got a NSPredicateEditor setup in my UI where the user can combine a NSPredicate to find a file. He should be able to filter by name, type, size and date.

There are several problems I have now:

  • I can only get one predicate object from the editor. When I use "predicateForRow:" it returns (null)
  • If the user wants to filter the file by name AND size or date, I can't just use this predicate on my array anymore because those information are not contained in it

Can I split up a predicate into different predicates without converting it into a NSString object, then search for every @" OR " | @" AND " and seperating the components into an array and then converting every NSString into a new predicate?


In the NSPredicateEditor settings I've some options for the "left Expression": Keypaths, Constant Values, Strings, Integer Numbers, Floating Point Numbers and Dates. I want to display a dropdown menu to the user with "name", "type", "date", "size". But then the generated predicate automatically looks like this:

"name" MATCHES[c] "nameTest" OR "type" MATCHES[c] "jpg" OR size == 100

Because the array is filled with strings, a search for "name", "type" etc. and those strings do not respond to @"myString"*.name*m the filter always returns 0 objects. Is there a way to show the Name, Type, Size and Date in the Menu, but write "self" into the predicate without doing it by hand?

I've already searched in the official Apple tutorials, on Stackoverflow, Google, and even Youtube to find a clue. This problem troubles me for almost one week now. Thanks for you time! If you need more information please let me know!

Это было полезно?

Решение

You have come to the right place! :)

I can only get one predicate object from the editor.

Correct. It is an NSPredicateEditor, not an NSPredicatesEditor. ;)

When I use "predicateForRow:" it returns (null)

I'm not sure I would use that method. My general rule of thumb is to largely ignore that NSPredicateEditor is a subclass of NSRuleEditor, mainly because it's such a highly specialized subclass that many of the superclass methods don't make that much sense on a predicate editor (like all the stuff about criteria, row selection, etc). It's possible that they're somehow relevant, but if they are, I haven't figured out how yet.

To get the predicate from the editor, you do:

NSPredicate *predicate = [myPredicateEditor objectValue];

If the user wants to filter the file by name AND size or date

You mean (name = [something]) AND (size = [something] OR date = [something])?

If so, NSPredicateEditor can do that if you've set the nesting mode to "Compound".

I can't just use this predicate on my array anymore because those information are not contained in it

What information do you need?

Can I split up a predicate into different predicates without converting it into a NSString object, then search for every @" OR " | @" AND " and seperating the components into an array and then converting every NSString into a new predicate?

Yes, but that is a BAD idea. It's bad because NSPredicate already contains all the information you need, and converting it to a different format and doing string manipulations just isn't necessary and can potentially lead to complications (like if someone can type in a value for "name", what happens if they type in " OR "?).

I'm having a hard time trying to figure out what it is you're trying to do. It sounds like you have an array of NSString objects that you want to filter based on a predicate that the user creates? If so, then what do these name, date, and size key paths mean? What are you trying to do?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top