Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C keypath to get all artists from iTunes

I'm using key-value-coding to get all the artists from iTunes:

[self.iTunes valueForKeyPath:@"[email protected][email protected]"];

Now, this works fine. This is very efficient. I would also like to do the same with the album.

[self.iTunes valueForKeyPath:@"[email protected][email protected]"];

The problem here is, that there are multiple albums with the same name, but not necessarily of the same artist. Is there a way to get a song of each album, so I can find out what artists it is, and also get the cover of it? I know there is NSPredicate, but this is very slow.

The specific code is not important, I only need the key-value coding part.

Thank you!

like image 827
IluTov Avatar asked Sep 10 '12 21:09

IluTov


1 Answers

This is not a complete answer.

For the benefit of folks like me who didn't originally know where this was documented: You have to be on a Mac with iTunes installed, and then run the command

sdef /Applications/iTunes.app | sdp -fh --basename iTunes

which will magically create iTunes.h in your current working directory. Then you can trawl through iTunes.h to see how the API looks. It doesn't seem to be officially documented in the Mac Developer Library or anything.

Each iTunesTrack has an artist property in addition to the album property, so I would think that all you really want to do is return an array of unique tuples (album, artist).

Okay, so how can we get an array of tuples out of a single KVC query? We'd like to write something like [email protected][email protected] and have that return something like an NSArray of NSDictionary, for example @[ @{ @"Help!", @"The Beatles" }, @{ @"No!", @"They Might Be Giants" }, ... ]

EDIT ughoavgfhw suggested the next step in a comment: You could use a category to define albumAndArtist like so:

@interface iTunesTrack (albumAndArtist)
@property (readonly) NSDictionary *albumAndArtist;
@end

@implementation iTunesTrack (albumAndArtist)
-(NSDictionary *) albumAndArtist
{
    return @{ @"album":self.album, @"artist":self.artist };
}
@end

And now you can write the line we were trying to write:

[self.iTunes valueForKeyPath:@"[email protected][email protected]"];

This should give you an NSArray of NSDictionary, which is basically what you wanted. Mind you, I haven't tested this code or anything!

like image 192
Quuxplusone Avatar answered Sep 18 '22 13:09

Quuxplusone