在我的应用中,我需要将数据存储在 plist 文件中,以便在另一个UIViewController
中使用,并在应用上保留一个小数据库。我这样做了:
NSDictionary
NSDictionary
NSMutableArray
ViewController.m
- (void)saveActivity {
if ([FileHander plistExist]) {
NSMutableArray *dataFromFile = [[NSMutableArray alloc]init];
dataFromFile = [FileHander loadDataFromFile];
if (![[dataFromFile lastObject]objectForKey:@"type"]) {
[FileHander saveFileWithArray:arrayActivity];
} else {
[dataFromFile addObject:arrayActivity];
[FileHander saveFileWithArray:dataFromFile];
}
} else {
NSLog(@"%@", arrayActivity);
[FileHander saveFileWithArray:arrayActivity];
}
}
arrayActivity
包含这个(坐标,速度,时间等......):
(
{
coordinates = (
"<+45.44083526,+9.19919774> +/- 10.00m (speed 0.00 mps / course 71.02) @ 04/02/14 12:46:08 Ora standard dell'Europa centrale",
"<+45.44083561,+9.19919755> +/- 65.00m (speed -1.00 mps / course -1.00) @ 04/02/14 12:46:33 Ora standard dell'Europa centrale",
"<+45.44083612,+9.19919728> +/- 65.00m (speed -1.00 mps / course -1.00) @ 04/02/14 12:46:33 Ora standard dell'Europa centrale",
"<+45.44083663,+9.19919700> +/- 65.00m (speed -1.00 mps / course -1.00) @ 04/02/14 12:46:33 Ora standard dell'Europa centrale",
"<+45.44083714,+9.19919673> +/- 65.00m (speed -1.00 mps / course -1.00) @ 04/02/14 12:46:33 Ora standard dell'Europa centrale",
"<+45.44084181,+9.19917132> +/- 65.00m (speed -1.00 mps / course -1.00) @ 04/02/14 12:46:33 Ora standard dell'Europa centrale",
"<+45.44083100,+9.19919440> +/- 65.00m (speed -1.00 mps / course -1.00) @ 04/02/14 12:46:33 Ora standard dell'Europa centrale",
"<+45.44088744,+9.19913454> +/- 50.00m (speed 0.00 mps / course -1.00) @ 04/02/14 12:46:35 Ora standard dell'Europa centrale",
"<+45.44088744,+9.19913454> +/- 30.00m (speed 0.00 mps / course -1.00) @ 04/02/14 12:46:36 Ora standard dell'Europa centrale",
"<+45.44088744,+9.19913454> +/- 30.00m (speed 0.00 mps / course -1.00) @ 04/02/14 12:46:37 Ora standard dell'Europa centrale",
"<+45.44088744,+9.19913454> +/- 30.00m (speed 0.00 mps / course -1.00) @ 04/02/14 12:46:39 Ora standard dell'Europa centrale"
);
speed = (
);
steps = 16;
time = "00:00:08";
type = Trekking;
}
)
实用程序类是这样的:
FileHandler.m
#import "FileHander.h"
@implementation FileHander
+ (BOOL) plistExist {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *path = [documentsDir stringByAppendingPathComponent:@"activity.plist"];
return [[NSFileManager defaultManager]fileExistsAtPath:path];
}
+ (NSMutableArray *) loadDataFromFile {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *path = [documentsDir stringByAppendingPathComponent:@"activity.plist"];
NSMutableArray *dataFound = [NSArray arrayWithContentsOfFile:path];
return dataFound;
}
+ (BOOL) deletePlist {
NSFileManager *manager = [NSFileManager defaultManager];
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"activity.plist"];
NSError *error;
BOOL success =[manager removeItemAtPath:filePath error:&error];
if (success) {
return YES;
}
else
{
return NO;
}
}
+ (void) saveFileWithArray: (NSMutableArray*)activityArray {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"activity.plist"];
[activityArray writeToFile:fileName atomically:YES];
}
@end
当我执行此应用程序时,它无法在设备上创建plist文件(我通过使用Mac App iExplorer查找此文件),我的代码有什么问题? 我使用相同的代码开发另一个应用程序,它工作得很好。 我希望你能帮助我解决这个问题。 谢谢
答案 0 :(得分:1)
请尝试下面的代码来创建plist文件..我已经尝试过并且工作正常..
使用NSARRAY创建文件:
- (void) saveFileWithArray: (NSMutableArray*)activityArray {
NSData * data = [NSKeyedArchiver archivedDataWithRootObject:activityArray];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *Path =[NSString stringWithFormat:@"%@/%@.plist",docDir,@"isLogin"];
NSLog(@"-->%@",Path); // PATH OF YOUR PLIST FILE
BOOL didWriteSuccessfull = [data writeToFile:Path atomically:YES];
if (didWriteSuccessfull) {
NSLog(@"store succsessfully");
}
else {
NSLog(@"Error in Storing");
}
}
从文件中获取NSARRAY:
- (NSMutableArray *) loadDataFromFile {
NSArray *myArray=[[NSArray alloc]init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *Path =[NSString stringWithFormat:@"%@/%@.plist",docDir,@"isLogin"];
if ([[NSFileManager defaultManager] fileExistsAtPath:Path])
{
NSLog(@"exist");
myArray=[[NSArray alloc]initWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:[NSData dataWithContentsOfFile:Path]]];
return [NSMutableArray arrayWithArray:myArray];
}
else{
NSLog(@"not exist");
return nil;
}
}