views:

301

answers:

2

Hi!

I have an Core Data entity called Album; an album has a relationship to Song entities (called songs), and each Song has an arbitrary count of Tag entities (in a tags property), tags have a name. Now I want to check whether any of the songs in an Album contain a Tag; using a single NSPredicate.

An ideas that came to mind was:

[NSPredicate predicateWithFormat: @"ANY (ANY songs).tags LIKE %@", someTagName];

But this causes an error at runtime ("Unable to parse the format string"). I tried various variations, without success.

Is there any way to do this?

A: 

I think you want

[NSPredicate predicateWithFormat:@"ANY [email protected] LIKE %@", someTagName]

Check out the Key-Value Programming Guide section on set and array operators.

Barry Wark
Sounded really good, I tried it, but I'm stuck again. This usually doesn't happen to me that often...Now I get the following exception:*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<MCAlbum 0x79a1760> valueForUndefinedKey:]: the entity Album is not key value coding-compliant for the key "[email protected]".'I tried round, but I just couldn't find a spot where I could put the @distinctUnionOfSets without causing that kind of crash.Any clue what I am doing wrong?
@Julian Sorry, this is my fault for writing without testing. Since I don't have a Core Data project to play with, I'm working off the top of my head. Did you try @unionOfSets, not @distinctUnionOfSets? I always struggle a bit with these set operators. I often have to put a breakpoint on a line like [album valueForKeyPath:@"@songs.tags"] and see what the result is in the debugger. If it's a set of sets, then @unionOfSets.songs.tags should do it. If it's something else, then you'll need to modify the keypath appropriately.
Barry Wark
A: 

Just a thought, but perhaps LIKE is not available on the iPhone? I know I had a hard time with BETWEEN, which doesn't appear to work.

So a tag does not have a many-to-many relationship with songs? If it was modeled that way, getting the songs for a tag would just involve accessing the property tag.songs. The way you have it modeled, it seems that tags can be duplicated for each song (which may be just fine in your app).

Dimitri