Skip to content

Instantly share code, notes, and snippets.

@nevyn
Created May 30, 2011 12:12
Show Gist options
  • Save nevyn/998818 to your computer and use it in GitHub Desktop.
Save nevyn/998818 to your computer and use it in GitHub Desktop.
Count of tracks in playlists in a playlist folder
#import <Foundation/Foundation.h>
@interface PlaylistFolder : NSObject
@property (retain) NSArray *playlists;
-(id)trackCount;
@end
@interface Playlist : NSObject
@property (retain) NSArray *tracks;
@end
@interface Track : NSObject
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
PlaylistFolder *folder = [PlaylistFolder new];
Playlist *pl1 = [Playlist new], *pl2 = [Playlist new], *pl3 = [Playlist new];
pl1.tracks = [NSArray arrayWithObjects:[Track new], nil];
pl2.tracks = [NSArray arrayWithObjects:[Track new], [Track new], nil];
pl3.tracks = [NSArray arrayWithObjects:[Track new], [Track new], [Track new], [Track new], nil];
folder.playlists = [NSArray arrayWithObjects:pl1, pl2, pl3, nil];
NSLog(@"Folder %@", [folder trackCount]);
[pool drain];
return 0;
}
@implementation Playlist
@synthesize tracks;
@end
@implementation Track
@end
@implementation PlaylistFolder
@synthesize playlists;
-(id)trackCount;
{
/////// This is the part I can't figure out
return [self valueForKeyPath:@"[email protected].@count"];
}
@end
@belkadan
Copy link

belkadan commented Jun 3, 2011

It's [email protected].@count.

AFAIK, @unionOfArrays will evaluate the entire remaining key-path, then union. So the original code tried to union three counts, rather than count a union. (Does that make sense?)

The new code counts each array in playlists.tracks and sums the array of counts.

(This is me randomly reading other programmers' Twitter feeds...)

@nevyn
Copy link
Author

nevyn commented Jun 3, 2011

@sum! Of course! That's what I get for coding when I'm tired. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment