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

我打电话从iPhone应用程序的Web界面,您可以通过server.Now数据,我想存储这些数据,因为它是locally.After goggling,我得到了可行的解决方案本地存储所有这些东西在plist文件,而不是将它存储在数据库中。

那么,什么我的问题是我会能够存储结构复杂或嵌套数组有嵌套字典到plist文件?

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


下面是我用来存放locally.Using这段代码的代码片段,得到的.plist应用,在模拟器访问的文件目录中创建成功。

-(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 muliple键值对,我可以写在同一个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文件中的问题。

提前致谢 ....

--------------解决方案-------------

尝试这样的

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);

}

如果您使用的NSLog错误结果必须是这样的错误SAVEDATA:属性列表无效的格式(属性列表不能包含类型'CFNull'的对象)

分类:iOS4的 时间:2015-03-15 人气:0
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 2.020 (s). 10 q(s)