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

问题描述 投票:1回答:2

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

所以,我能否将复杂结构或具有嵌套字典的嵌套数组存储到plist文件中?

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS80azBQYS5wbmcifQ==” alt =“在此处输入图像描述”>

下面是我用于本地存储的代码段。使用此代码,.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的数据。但是文件上没有任何内容。如果我使用带多个键值对的字典,则可以在同一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];

但是相同的代码不适用于其中已解析了内容的我的数组。

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文件的问题在哪里?

ios4 file-io iphone-sdk-3.0 plist
2个回答
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);

}

2
投票

如果使用NSLog,错误结果必须类似于以下内容saveData中的错误:属性列表对于格式无效(属性列表不能包含'CFNull'类型的对象)

© www.soinside.com 2019 - 2024. All rights reserved.