问题将json保存到.plist

时间:2011-05-26 14:09:22

标签: iphone xcode json

-(IBAction)buttonlogin:(id)sender
{



    NSString *samplejson=@"http://www.colorysabor.com/sample.json";

    NSData *flickrData=[NSData dataWithContentsOfURL:[NSURL URLWithString:samplejson]];
    NSError *theError = nil;

    NSMutableDictionary *theObject = [[NSMutableDictionary alloc] init];
    theObject =[[CJSONDeserializer deserializer] deserialize:flickrData error:&theError];



NSLog(@"Result: %@",theObject);

    NSBundle *bundle = [NSBundle mainBundle];
    NSString *filePath = [bundle pathForResource:@"data" ofType:@"plist"];


    [theObject writeToFile:filePath atomically:YES];

我打算将json解析保存到data.plist以供稍后使用,但不保存

2 个答案:

答案 0 :(得分:3)

我尝试了两种方法。我得到了以下结果:

filepath(1):/ Users / user / Library / Application Support / iPhone Simulator / 5.0 / Applications / CFADAD22-F041-4F6D-9B50-70C140372D06 / FlipView.app / apiData.plist

filepath(2):/ Users / user / Library / Application Support / iPhone Simulator / 5.0 / Applications / CFADAD22-F041-4F6D-9B50-70C140372D06 / Documents / apiData.plist

但我找不到任何位置的plist文件。

这里可能有什么问题?

答案 1 :(得分:1)

方法pathForResource:ofType:用于检索现有文件/资源​​的路径,而不是用于创建新的文件路径

试试这个:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myFile.plist"];
[myDictionary writeToFile:path atomically:YES];