开发者

what is the best method to store an array of objects on NSUserDefaults

I want to store xml on the iphone that consists of a number of favourite items chosen by the user of the app. I am using TBXML (inherited from prev dev) and as such this cant write xml, I was thinking of chopping together the xml from each item as a string and then loading this in Like so:

tbxml = [[TBXML alloc] initWithXMLString:myXML]

This is not ideal but is there limitations / advice for this method and what would be the best place to store this file?

Many thanks

edit

The user favourites an item in a UITableView, these can then be viewed in another UITableView called 'My Favourites'. The UITableViewController displays the content from an xml file returned from a server. I was trying to make things simple for me to effectively append each 'items' xml to a large string and then store that string somewhere - either in a file or in NSUserDefaults

ideally I could save an array of objects as they are added to favourites to be read back in later.

the objects are created like so

@interface Playlist : NSObject {

NSURL *imagePath;
NSString *title;
NSString *subTitle;
NSString *mediaType;
NSString *artistName;
NSString *id;

}


@property (nonatomic, retain) NSURL *imagePath;
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *subTitle;
@property (nonatomic, retain) NSString *mediaType;
@property (nonatomic, retain) NSString *artistName;
@property (nonatomic, retain) NSString *id;

@end

is there a way of saving an array of objects like the one above in NSUserDefaults?

thanks

EDIT 2 I went with NSUserDefaults and maintained an ID for each item as an array

very easy to access and update, I made a PlaylistDataController class with methods in there like this one t开发者_运维问答o add an item to my 'MyPlaylistArray' in the user defaults

-(void) addItemToPlaylist: (NSString*) myID
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSMutableArray *myPlaylistArray = [NSMutableArray arrayWithArray:[defaults objectForKey:@"MyPlaylistArray"]];
    [myPlaylistArray addObject:myID];
    [defaults setObject:myPlaylistArray forKey:@"MyPlaylistArray"];
    [defaults synchronize];

}

Hope it helps someone


I can't really tell from the information you've given but "favourite items chosen by user" sounds like NSUserDefaults, which would be a lot easier than xml.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜