Created
May 30, 2011 12:12
-
-
Save nevyn/998818 to your computer and use it in GitHub Desktop.
Count of tracks in playlists in a playlist folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
I've tried a bunch of different variants but I can't seem to get it right.
I ended up doing
return [self valueForKeyPath:@"[email protected]"];
(since I had the attribute countOfTracksCached; still not sure how I would do it without exposing the count as a separate property)
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...)
@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
2011-05-30 14:14:15.674 Foo[95534:a0f] -[NSCFNumber count]: unrecognized selector sent to instance 0x100106ae0
2011-05-30 14:14:15.676 Foo[95534:a0f] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFNumber count]: unrecognized selector sent to instance 0x100106ae0'
*** Call stack at first throw:
(
0 CoreFoundation 0x00007fff820eb7b4 exceptionPreprocess + 180
1 libobjc.A.dylib 0x00007fff84e7b0f3 objc_exception_throw + 45
2 CoreFoundation 0x00007fff82145110 +[NSObject(NSObject) doesNotRecognizeSelector:] + 0
3 CoreFoundation 0x00007fff820bd91f __forwarding + 751
4 CoreFoundation 0x00007fff820b9a68 _CF_forwarding_prep_0 + 232
5 CoreFoundation 0x00007fff82096c47 -[NSMutableArray addObjectsFromArray:] + 71
6 Foundation 0x00007fff800ef5b4 -[NSArray(NSKeyValueCoding) _unionOfArraysForKeyPath:] + 112
7 Foundation 0x00007fff8007ec2f -[NSArray(NSKeyValueCoding) valueForKeyPath:] + 566
8 Foundation 0x00007fff80022e62 -[NSObject(NSKeyValueCoding) valueForKeyPath:] + 376
9 Foo 0x0000000100001c56 -[PlaylistFolder trackCount] + 45
10 Foo 0x0000000100001b6c main + 616
11 Foo 0x00000001000018fc start + 52
12 ??? 0x0000000000000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'