Ios 无法将数据保存到Plist?
嗨,我有一个Ios 无法将数据保存到Plist?,ios,objective-c,nsmutablearray,plist,nsmutabledictionary,Ios,Objective C,Nsmutablearray,Plist,Nsmutabledictionary,嗨,我有一个NSMutableDictionary,我添加了5个带键的对象(纬度、经度、强度、图像、元图像),然后我尝试将其添加到NSMutableArray中,并将NSMutableArray写入.plist文件。但事实上,这不是写作。我的.plist文件仍然为空 请检查我的代码: paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); filePath = [[paths
NSMutableDictionary
,我添加了5个带键的对象(纬度、经度、强度、图像、元图像),然后我尝试将其添加到NSMutableArray
中,并将NSMutableArray
写入.plist文件。但事实上,这不是写作。我的.plist文件仍然为空
请检查我的代码:
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"history.plist"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
if([latitude length]>0 && [longitude length]>0){
[dictValue setObject:latitude forKey:@"latitude"];
[dictValue setObject:longitude forKey:@"longitude"];
}
[dictValue setObject:metaImage forKey:@"imageData"];
[dictValue setObject:image forKey:@"image"];
if([_intensity.text length]>0){
[dictValue setObject:_intensity.text forKey:@"intensity"];
}else{
intensityAlert=[[UIAlertView alloc]initWithTitle:@"Beta App" message:@"Please select intensity." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[intensityAlert show];
}
if(fileExists){
finalArray=[[NSMutableArray alloc]initWithContentsOfFile:filePath];
[finalArray addObject:dictValue];
NSLog(@"dd%@",dictValue);
}else{
[finalArray addObject:dictValue];
NSLog(@"new%@",finalArray);
}
[finalArray writeToFile:filePath atomically:YES];
之后,我使用以下代码检查plist中的数据
finalArray=[[NSMutableArray alloc]initWithContentsOfFile:filePath];
NSLog(@"server%@",finalArray);
它打印的是零值。让我们试试:
if(fileExists){
finalArray=[[NSMutableArray alloc]initWithContentsOfFile:filePath];
[finalArray addObject:dictValue];
NSLog(@"dd%@",dictValue);
}else{
// let be sure that final array allocated : [[NSMutableArray alloc] initWith...]
[finalArray addObject:dictValue];
NSLog(@"new%@",finalArray);
}
-(BOOL)writeToFile:(NSString*)路径原子:(BOOL)标志的文档
:
此方法递归地验证所有包含的对象都是
属性列表对象(NSData、NSDate、NSNumber、,
NSString、NSArray或NSDictionary),然后再写出文件,以及
如果所有对象都不是属性列表对象,则返回否,因为
结果文件将不是有效的属性列表
您可以尝试将图像保存到文件夹中,并将图像相对文件路径保存到plist文件。什么数据类型是
metaImage
和image
?如果不先将UIImage
对象转换为NSData
.UIImage,则无法将其存储在plist中,因此我需要将其转换为NSData ryt?如果“ryt”的意思是“正确”,则是,您需要将UIImage
转换为NSData
,并将NSData
放入字典中;可能是因为您试图在数组中存储非plist值,所以从未写入该文件。检查writeToFile:atomicaly:
的返回值。如果返回NO
,则表示文件未保存。数组中只能有plist兼容的值。