Search code examples
iphonensmutabledictionarympmediaitem

Save MPMediaItem into a NSMutableDictionary


I am trying to save the MPMediaItem in nsmutableDictionary..

Actually my purpose is to let the user select his songs and then have it saved in the app and then whenever he wants he can play.

i have added this code for saving the MPMediaItem to Dictionary

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:mediaItemCollection.representativeItem  forKey:@"name"];

and added it in a NSMutableArray and then that array into NSUserDefaults.

but when i try to do this i get this error:

[NSUserDefaults setObject:forKey:]: 

Attempt to insert non-property value '(
        {
        desc = Test;
        group = Main;
        msg = Test;
        name = test;
        rec =         (
                        {
                name = "<MPConcreteMediaItem: 0x12b040> 8240103918041924793";
                number = test;
            }
        );
        type = "Play File";
    }
)' of class '__NSArrayM'.  Note that dictionaries and arrays in property lists must also contain only property values.

Solution

  • You shouldn't be saving MPMediaItems (I don't even know if these have keyed archivers & unarchivers associated with them), but save the unique identifier (referred to via MPMediaItemPropertyPesistentID property keys) associated with each item.

    Check out the answers to this closely related question and you might find a path to get to where you want to go.