Ios 为什么不是';t my.plist是否显示保存的数据?
我有一个简单的待办事项清单,在清单中添加项目。我想在应用程序关闭时将这些项目保存到.plist文件中,以便在用户重新打开应用程序时显示这些项目 我目前正在尝试两种不同的方法。他们都给了我成功保存的Ios 为什么不是';t my.plist是否显示保存的数据?,ios,objective-c,nsmutablearray,plist,writetofile,Ios,Objective C,Nsmutablearray,Plist,Writetofile,我有一个简单的待办事项清单,在清单中添加项目。我想在应用程序关闭时将这些项目保存到.plist文件中,以便在用户重新打开应用程序时显示这些项目 我目前正在尝试两种不同的方法。他们都给了我成功保存的列表日志条目,但在重新启动时都不显示文件 方法1: 在mymainAppDelegate.m - (void)applicationWillTerminate:(UIApplication *)application { NSArray *paths = NSSearchPathForDirec
列表代码>日志条目,但在重新启动时都不显示文件
方法1:
在mymainAppDelegate.m
- (void)applicationWillTerminate:(UIApplication *)application
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *filePath = [basePath stringByAppendingPathComponent:@"toDoItems.plist"];
NSMutableDictionary *plistdict = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
[plistdict writeToFile:filePath atomically:YES];
if(![self.toDoItems writeToFile:filePath atomically:YES]) {
NSLog(@"List Successfully Saved!");
};
}
在myXYZToDoListController.m
中:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"toDoItems.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:filePath])
- (void)applicationWillTerminate:(UIApplication *)application
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"toDoItems.plist"];
if(![self.toDoItems writeToFile:filePath atomically:YES]) {
NSLog(@"List Successfully Saved!");
};
}
方法2:
在mymainAppDelegate.m
中:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"toDoItems.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:filePath])
- (void)applicationWillTerminate:(UIApplication *)application
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"toDoItems.plist"];
if(![self.toDoItems writeToFile:filePath atomically:YES]) {
NSLog(@"List Successfully Saved!");
};
}
在myXYZToDoListViewController
中:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"toDoItems.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:filePath])
{
self.toDoItems = [[NSMutableArray alloc]initWithContentsOfFile:filePath];
NSLog(@"Loading saved list");
} else {
self.toDoItems = [[NSMutableArray alloc] init];
NSLog(@"Empty List");
}
}
我对使用objective C相当陌生,我已经阅读了很多问题和答案,但我似乎无法加载.plist文件。有没有人能帮我找出代码中的错误,并给出一个适用于我的情况的好例子
正如我所说,在日志输出中,我看到“Empty List”方法正在加载,它告诉我“List已成功保存!”。除此之外的任何调试输出我都不知道如何使用
谢谢您将文件保存在NSDocumentsDirectory(视情况而定)中,但当您加载它时,您只是从应用程序当前目录(可能是应用程序文件夹本身)加载它。在读取之前,您正在写入一个空文件,请尝试在应用程序代理中使用以下命令:
-(NSString*)toDoFilePath
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *filePath = [basePath stringByAppendingPathComponent:@"toDoItems.plist"];
return filePath;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
if ([[NSFileManager defaultManager] fileExistsAtPath:[self toDoFilePath]])
{
self.toDoItems = [[NSMutableArray alloc]initWithContentsOfFile:[self toDoFilePath]];
NSLog(@"Loading saved list");
} else {
self.toDoItems = [[NSMutableArray alloc] init];
NSLog(@"Empty List");
}
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
if([self.toDoItems writeToFile:[self toDoFilePath] atomically:YES]) {
NSLog(@"List Successfully Saved!");
};
}
假设(看起来)您的应用程序委托上已经有toDoItems属性
要使用视图控制器中的数据,在包含AppDelegate.h文件后,可以使用:
self.toDoItems = [(AppDelegate*) [[UIApplication sharedApplication] delegate] toDoItems];
更好的是,只需调用一个app delegate方法即可返回plist文件的路径,并将所有加载/保存操作移动到app delegate中。请提供一个示例,说明您在评论中的意思?你是说通过mainAppDelegate加载数据?我尝试使用我发布的两种方法在您编写时添加NSDocumentDirectory路径,但我仍然无法加载该文件。再次查看您的代码,您也在编写之前读取了(不存在的)文件。通过编辑,我在输出中没有看到任何“成功保存列表!”日志。因为当我复制您的代码时,我复制了你的错误成功检查,writeToFile在成功时返回true,而不是false。我已经修正了这个例子。另外,最好不要在applicationWillTerminate中保存数据,而是在数据更新时保存,但我将其保留下来作为读者的练习:)