我正在尝试获取iTunes中所有播放列表的列表,并将它们放在用户可以选择的弹出按钮中。

我创建了一个自定义类来与iTunes进行接口,将播放列表拉入,然后将其塞入NSMutableDictionary中。

然后,我在AppDelegate中将我的iTunes控制器类实例化为“ iTunesInterface”。

在我的.xib文件中,我创建了一个Dictionary Controller并将其与模型键路径iTunesInterface.userPlaylists绑定到AppDelegate。

然后,我选择了弹出按钮,并将内容和内容值绑定到词典控制器的排列对象上。

一切都可以编译,但是我无法在弹出按钮中显示任何内容。完全是空的。不知道我在做什么错。这是代码:

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


AppDelegate.h的相关部分

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

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

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


AppDelegate.m的相关部分

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


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


我为此大吃一惊,无法弄清楚为什么我的值没有显示在弹出按钮中。有什么建议么?在此先感谢您的帮助!

最佳答案

由于从未初始化playlistNames,因此iController.m中实现的TuneController::playlists始终将始终返回nil。

关于objective-c - 使NSMutableDictionary中的键显示在弹出按钮中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9346003/

10-08 21:43