在设备上保存和检索信息

Saving and retrieving information on device

这是一个简单的项目,我正在从书上复制一半来自学。这是一个待办事项列表,有一个 table 视图、一个文本字段和一个用于将任务添加到列表的按钮。然后它将任务数组保存到一个目录(使用辅助函数),当您关闭并重新打开应用程序时,它会检索信息。这个东西在计算机上运行,​​我认为它也可以在设备上运行,直到我关闭应用程序并重新打开它以发现那里的数组元素 none。当它在计算机上运行时,我不知道有什么不对。一些我理解的代码,一些我只是暂时复制的代码(比如辅助函数),非常感谢任何建议! P

代码:

h 文件

#import <UIKit/UIKit.h>

NSString *docPath();

@interface ViewController : UIViewController<UITableViewDataSource>

{
    UITableView *taskTable;
    UITextField *taskField;
    UIButton *insertButton;

    NSMutableArray *tasks;

}

- (void)addTask:(id)sender;

- (void)writeTasksToFile:(NSString *)path;

@end

米文件

#import "ViewController.h"

NSString *docPath()
{
    NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    return [[pathList objectAtIndex:0] stringByAppendingString:@"data.td"];
}

@interface ViewController ()

@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSArray *plist = [NSArray arrayWithContentsOfFile:docPath()];
    if(plist) {
        tasks = [plist mutableCopy];
    } else {
        tasks = [[NSMutableArray alloc] init];
    }

    CGRect buttonFrame = CGRectMake(228,40,72,31);
    insertButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [insertButton setFrame:buttonFrame];
    insertButton.backgroundColor = [UIColor redColor];
    [insertButton addTarget:self action:@selector(addTask:) forControlEvents:UIControlEventTouchUpInside];
    [insertButton setTitle:@"Insert" forState:UIControlStateNormal];
    [[self view] addSubview:insertButton];

    CGRect tableFrame = CGRectMake(0, 80, 320, 380);
    taskTable = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
    [taskTable setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    taskTable.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:taskTable];
    [taskTable setDataSource:self];

    CGRect fieldFrame = CGRectMake(20, 40, 200, 31);
    taskField = [[UITextField alloc]initWithFrame:fieldFrame];
    [taskField setBorderStyle:UITextBorderStyleRoundedRect];
    [taskField setPlaceholder:@"type a task, enter"];
    [taskField setBackgroundColor:[UIColor greenColor]];
    [self.view addSubview:taskField];


    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];

}

- (void) applicationDidEnterBackground:(UIApplication *)application {
    [tasks writeToFile:docPath() atomically:YES];

}

- (void)addTask:(id)sender{

    NSString *t = [taskField text];

    if ([t isEqualToString:@""]) {return;}

    [tasks addObject:t];
    [taskTable reloadData];
    [taskField setText:@""];
    [taskField resignFirstResponder];

}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [tasks count];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [tasks removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    } 
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *c = [taskTable dequeueReusableCellWithIdentifier:@"Cell"];

    if (!c){
        c = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }

    NSString *item = [tasks objectAtIndex:[indexPath row]];
    [[c textLabel] setText:item];

    return c;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)writeTasksToFile:(NSString *)path
{
    [tasks writeToFile:path atomically:YES];
}

@end

编辑---------------------------------------- ---------------------------------------------- ------------------------------

这是调试器屏幕截图,在检查是否有 plist 的行上有一个断点。

我看到需要更改的一件事是您的 docPath() 返回了错误的路径。您需要使用 stringByAppendingPathComponent 而不是 stringByAppendingString

return [[pathList objectAtIndex:0] stringByAppendingPathComponent:@"data.td"];

或者,如果你想使用 stringByAppendingString 那么它应该是:

return [[pathList objectAtIndex:0] stringByAppendingString:@"/data.td"];