如何在.plist文件中存储嵌套数组/ JSON字符串?

时间:2012-02-02 12:02:23

标签: ios4 file-io iphone-sdk-3.0 plist

我正在从iPhone应用程序调用一个Web界面来从服务器获取数据。现在我想存储这些数据,因为它是本地的。在goggling之后,我得到了可行的解决方案来存储所有这些东西本地在plist文件中,而不是将其存储在数据库中。

那么,我的问题是我能将复杂结构或嵌套数据存储到plist文件中吗?

enter image description here

下面是我用来在本地存储它的代码片段。使用此代码,.plist在应用程序的文档目录中成功创建,可在Simulator中访问。

-(NSString *)Return_PlistPathCreation
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *libraryPath = [paths objectAtIndex:0];    
    NSString *plistPath = [libraryPath stringByAppendingPathComponent:@"LoadedNews.plist"];

    // Checks if the file exists at the writable location.
    if ( ![[NSFileManager defaultManager] fileExistsAtPath:plistPath] ) {
        NSString *masterFilePath = [[NSBundle mainBundle] pathForResource:@"LoadedNews" ofType:@"plist"];

        // Try to copy the master file to the writable location
        NSError *error;
        if ( ![[NSFileManager defaultManager] copyItemAtPath:masterFilePath toPath:plistPath error:&error] ) {
            NSLog(@"Error: %@", [error localizedDescription]); 
            // Serious error.
        }

    }
    return plistPath;
}

在代码下面编写Array.But的数据,没有任何东西写在文件上。如果我使用带有多个密钥值对的Dicrtionary,我可以在同一个plist上写内容文件。

//This code works perfectly.
NSArray *arr =[NSArray arrayWithObjects:@"ajay",@"sharma",nil];
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setValue:arr forKey:@"FullName"];
[[dict JSONRepresentation] writeToFile:plistPath atomically:YES encoding:NSASCIIStringEncoding error:nil];

但是相同的代码不能用于已解析其中内容的Array。

NSArray *arr =[NSArray arrayWithArray:ComplexDataArray];
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setValue:arr forKey:@"Data"];
[[dict JSONRepresentation] writeToFile:plistPath atomically:YES encoding:NSASCIIStringEncoding error:nil];

有人可以指导我将数据写入.plist文件的问题。

提前致谢....

2 个答案:

答案 0 :(得分:2)

如果你使用NSLog错误结果必须看起来像saveData中的错误:属性列表格式无效(属性列表不能包含'CFNull'类型的对象)

答案 1 :(得分:2)

试试这个

  NSMutableArray *types = [NSMutableArray array];


for (int i=0; i<jsonArray.count; i++) {
   NSString *t_ID = [[jsonArray objectAtIndex:i] valueForKey:@"id"];
   NSString *image = [[jsonArray objectAtIndex:i] valueForKey:@"image"];
   NSString *name =[[jsonArray objectAtIndex:i] valueForKey:@"name"];
   NSArray *dictArray = [NSArray arrayWithObjects:t_ID,image,name, nil];
   NSArray *dictKeys = [NSArray arrayWithObjects:@"id",@"image",@"name", nil];
 NSDictionary*  dict = [NSDictionary dictionaryWithObjects:dictArray forKeys:dictKeys];
   [types addObject:dict];
}
NSDictionary*  plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects:types, nil] forKeys:[NSArray arrayWithObjects:@"ptype", nil]];
NSString *error = nil;
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];


if(plistData)
{
    [plistData writeToFile:appDelegate.plistPath atomically:YES];
}
else
{
    NSLog(@"Error log: %@", error);

}