开发者

load data to plist with NSKeyedUnarchiver

i build an app that save data开发者_高级运维 to plist, i used the belwo code :

self.emaillist = [[NSMutableArray alloc]init ];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Email.plist"];


self.emaillist = [NSKeyedUnarchiver unarchiveObjectWithFile:finalPath];

and when the app comes to the last line it got crashed.


NSBundle have convenience method for finding the path for a resource like so:

NSString* path = [[NSBundle mainBundle] pathForResource:@"Email" 
                                                 ofType:@"plist"];

Armed with this path you also have a convenience method for extracting the property list. If you know the root of the plist is an array then just use:

NSArray* plist = [NSArray arrayWithContentsOfFile:path];
self.emails = [NSMutableArray arrayWithArray:plist];

Equivalent methods exist for dictionaries and strings.


You need to use the NSKeyedArchiver, not the unarchiver, to save data to a file.


It seems unlikely that your path is correct. I assume that your Plist is in the app's resources? In that case, the path to it would be:

[[NSBundle mainBundle] pathForResource:@"Email" ofType:@"plist"]
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜