-1

目前,我尝试制作一个音乐播放列表应用程序。我尝试从 NSUserDefaults 保存和检索播放列表数据。保存数据时我没有问题,但检索数据时出现错误。

我有:

由于未捕获的异常'MPMediaItemCollectionInitException'而终止应用程序,原因:'-init 不受支持,使用 -initWithItems:

我正在关注从保存的 persistentid 列表中检索的 Play iPod 播放列表中的答案,但我尝试用 swift 编写它。

这是我的保存功能:

func savePlaylist(var mediaItemCollection: MPMediaItemCollection){

    var items: NSArray = mediaItemCollection.items

    var listToSave: NSMutableArray = NSMutableArray()

    for song in items{

        var persistentId: AnyObject! = song.valueForProperty(MPMediaItemPropertyPersistentID)

        listToSave.addObject(persistentId)

    }

    var data: NSData = NSKeyedArchiver.archivedDataWithRootObject(listToSave)

    NSUserDefaults.standardUserDefaults().setObject(data, forKey: "songsList")
    NSUserDefaults.standardUserDefaults().synchronize()

}

这是我的检索功能:

func setupMusic(){

    if NSUserDefaults.standardUserDefaults().objectForKey("songsList") != nil{

        var theList: NSMutableArray = NSMutableArray()

        var data: NSData = (NSUserDefaults.standardUserDefaults().objectForKey("songsList") as? NSData)!

        var decodedData: NSArray  = (NSKeyedUnarchiver.unarchiveObjectWithData(data) as? NSArray)!

        theList.addObjectsFromArray(decodedData as [AnyObject])

        var allTheSongs: NSMutableArray = NSMutableArray()

        for var i = 0; i < theList.count; i++ {

            var songQuery = MPMediaQuery.songsQuery()

            var songs: NSArray = songQuery.items

            allTheSongs.addObject(songs)

        }

        var currentQueue: MPMediaItemCollection = MPMediaItemCollection()

        myMusicPlayer?.setQueueWithItemCollection(currentQueue)


    }else{
        println("fail!!!!!!!!!!!!")
    }

}

我该如何解决?

4

2 回答 2

1

这是问题var currentQueue: MPMediaItemCollection = MPMediaItemCollection()

你必须init带物品

根据苹果的文档

init(items:) 指定初始化器

使用媒体项数组初始化媒体项集合。宣言

迅速

init!(items items: [AnyObject]!) 参数 items

您分配给媒体项目集合的项目数组。

你必须用像这样的项目来初始化

var currentQueue: MPMediaItemCollection = MPMediaItemCollection(items:itemsArray)
于 2015-06-01T04:57:52.167 回答
0

我为您提供了一种非常简单的方法来获取任何类型的对象并从NSUserDefault. 您不需要显式使用 archive 和 unarchive 。只需创建一个 NSUserDefaults 类别如下

#import <Foundation/Foundation.h>
@interface NSUserDefaults (AKSNSUserDefaults)
- (void)saveCustomObject:(id<NSCoding>)object
                     key:(NSString *)key;
- (id<NSCoding>)loadCustomObjectWithKey:(NSString *)key;
@end  

@implementation NSUserDefaults (AKSNSUserDefaults)

- (void)saveCustomObject:(id<NSCoding>)object
                     key:(NSString *)key {
    NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:object];
    [self setObject:encodedObject forKey:key];
    [self synchronize];

}

- (id)loadCustomObjectWithKey:(NSString *)key {
    NSData *encodedObject = [self objectForKey:key];
    id object = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
    return object;
}

@end

并且您的 NSUserDefaults 已准备好存储任何对象。但有一件事是需要NSCoder在您的对象模型中使用。

例如 :

#import <Foundation/Foundation.h>

@interface AKSClassA : NSObject<NSCoding>
// declared some property 
@property (nonatomic,assign) NSInteger userID;
@property (nonatomic,strong) NSString *userName;
@end
#import "AKSClassA.h"
@implementation AKSClassA
@synthesize userID;
@synthesize userName;
-(id)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super init]) {

        self. userID = [aDecoder decodeIntegerForKey:@"userID"];
        self. userName = [aDecoder decodeObjectForKey:@"userName"];

   }
    return self;
}
-(void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeInteger:userID  forKey:@"userID"];
    [aCoder encodeObject:userName forKey:@"userName"];
}
@end
// in some class if you are using classA object member to store and retrieve with value  example below 
// to save data in NSUserDefaults 
  AKSClassA *obj =[array1 objectAtIndex:index];
  [[NSUserDefaults standardUserDefaults] saveCustomObject:button key:@"userData"];

// to retrieve data 
 AKSClassA *obj1 = (AKSClassA*)[[NSUserDefaults standardUserDefaults]loadCustomObjectWithKey:@"userData"];

    [titleLabel  setText:obj1.userName];
于 2015-06-01T06:53:47.450 回答