0

I've got an array like that

Word array (
    {
    translation =         (
                    {
            name = Roma;
            lang = it;
        },
                    {
            name = Rome;
            lang = en;
        }
    );
    type = provenance;
    value = RMU;
},
    {
    translation =         (
                    {
            name = "Milano";
            lang = it;
        },
                    {
            name = "Milan";
            lang = en;
        }
    );
    type = destination;
    value = MIL;
},)

The idea is to filter it using an NSPredicate and receive and an array of dictionaries based on the lang key, I'd like to get something like this made by filtering for lang == it,

Word array (
        {
            name = Roma;
            lang = it;
            type = provenance;
            value = RMU;
        },
        {
            name = "Milano";
            lang = it;
            type = destination;
            value = MIL;
        })

I can't simplify the data because it comes from a "JSON" service. I've tried different predicates using SUBQUERY but none of them works, documentation about SUBQUERY is pretty poor, I'm missing something, probably the problem is that I'd like to receive an object that is really different from the source.
Of course I'm able to obtain that structure enumerating, I'm wondering if there is a shorter solution
This answer from Dave DeLong link to SUBQUERY explanation gave a me a lot of hints about SUBQUERY, but I'm not able to find a solution to my problem.
Can someone give me a hints about?

1 Answer 1

1

You can't do this with a predicate. (Well, you could, but it would be stupidly complex, difficult to understand and maintain, and in the end it would be easier to write the code yourself)

NSPredicate is for extracting a subset of data from an existing set. It only* does filtering, because a predicate is simply a statement that evaluates to true or false. If you have a collection and filter it with a predicate, then what happens is the collection starts iterating over its elements and asks the predicate: "does this pass your test?" "does this pass your test?" "does this pass your test?"... Every time that the predicate answers "yes this passes my test", the collection adds that object to a new collection. It is that new collection that is returned from the filter method.

THUS:

NSPredicate does not (easily) allow for merging two sets of data (which is what you're asking for). It is possible (because you can do pretty much anything with a FUNCTION() expression), but it makes for inherently unreadable predicates.

SO:

Don't use NSPredicate to merge your dataset. Do it yourself.

1
  • Thanks Dave I was thinking almost the same,but now I'm sure. BTW since NSPredicate is a big lack in my preparation I must learn it soon or later. Could you suggest me a good tutorial as a starting point?thx
    – Andrea
    Mar 28, 2012 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.