Search code examples
objective-cmacoscocoaxcode4.2itunes

Getting Keys in NSMutable Dictionary to Appear in Pop Up Button


I'm trying to get a list of all playlists in iTunes and place them in a pop up button that the user can pick from.

I've created a custom class to interface with iTunes, gotten the playlists pulled in, and stuffed them in a NSMutableDictionary.

I then instantiated my iTunes controller class in AppDelegate as "iTunesInterface".

In my .xib file I created a Dictionary Controller and bound it to AppDelegate with the model key path iTunesInterface.userPlaylists

I then selected the pop up button and bound the content and content values to the Dictionary Controller's arranged objects...

Everything compiles, but I can't get anything to show up in the pop-up button. It's totally empty. Not sure what I'm doing wrong. Here's the code:

iController.h:

#import <Foundation/Foundation.h>
#import "iTunes.h"

@interface TuneController : NSObject
{
    iTunesApplication *iTunes;
    NSMutableDictionary *userPlaylists;
}

@property (retain, nonatomic) iTunesApplication *iTunes;
@property (copy, nonatomic) NSMutableDictionary *userPlaylists;

- (NSMutableDictionary *) playlists;


@end

iController.m

#import "iController.h"

@implementation TuneController

@synthesize iTunes;
@synthesize userPlaylists;


- (id) init {
    self = [super init];
    if (self)
    {
        // Create iTunes Object
        iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
        userPlaylists = [self playlists];
    }
    return self;
}

- (NSMutableDictionary *) playlists {  

    NSArray *sources = [iTunes sources];
    iTunesSource *librarySource = nil;

    for (iTunesSource *source in sources) {
        if ([source kind] == iTunesESrcLibrary) {
            librarySource = source;
            break;
        }
    }

    SBElementArray *playlists = [librarySource userPlaylists];
    NSMutableDictionary *playlistNames = nil;
    int i = 0;

    for (SBElementArray *list in playlists) {
        [playlistNames setObject:[playlists objectAtIndex:i] forKey:[[playlists objectAtIndex:i] name]];
        NSLog(@"Playlist Name: %@", [[playlists objectAtIndex:i] name]); // This is how I know I'm getting good values for the dictionary...
        i++; 
    } 

    return playlistNames; 

} 

@end

Relevant Section of AppDelegate.h

#import <Cocoa/Cocoa.h>
#import "iController.h"

@interface SCAppDelegate : NSObject <NSApplicationDelegate>
{
    TuneController *iTunesInterface;
}

@property (copy, nonatomic) TuneController *iTunesInterface;
@end

Relevant Section of AppDelegate.m

#import "SCAppDelegate.h"
#import "iController.h"


@implementation SCAppDelegate
...
@synthesize iTunesInterface;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    iTunesInterface = [[TuneController alloc] init];
}

I'm banging my head against this and can't figure out why my values aren't showing up in the pop-up button. Any suggestions? Thanks in advance for your help!


Solution

  • TuneController::playlists as implemented in iController.m will always return nil at the moment, as playlistNames is never initialized.