0

我正在制作一个任务管理器 iOS 应用程序。您键入一个任务,它会保存在一个数组中。我希望人们能够在手机之间共享任务,所以我添加了一种保存每个数组的方法。

现在我在本地使用我的想法。该页面有标题和密码。当按下保存按钮时,数组将保存到标题和密码唯一的文件中(效果很好,每次都保存)。

我需要找到一种方法,然后将文件中的所有信息返回到数组中,以便可以看到它。这是我尝试过的,请记住,除了“获取任务按钮”之外,一切正常,我的问题是getFile无效的:

BNRAppDelegate.m

#import "BNRAppDelegate.h"

NSString *docPath()
{
    NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                            NSUserDomainMask,
                                                            YES);
    return [[pathList objectAtIndex:0] stringByAppendingPathComponent:@"data.td" ];
}

@implementation BNRAppDelegate

@synthesize window = _window;

#pragma mark - Application delegate callbacks

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{
    return YES;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary     *)launchOptions
{
    NSArray *plist = [NSArray arrayWithContentsOfFile:docPath()];
    if (plist)
    {
        tasks = [plist mutableCopy];
    }
    else
    {
        tasks = [[NSMutableArray alloc] init];
    }

    CGRect windowFrame = [[UIScreen mainScreen] bounds];
    UIWindow *theWindow = [[UIWindow alloc] initWithFrame:windowFrame];
    [self setWindow:theWindow];

    [[self window] addSubview:taskTable];
    [[self window] addSubview:taskField];
    [[self window] addSubview:titleField];
    [[self window] addSubview:insertButton];
    [[self window] addSubview:clearButton];
    [[self window] addSubview:shareButton];
    [[self window] addSubview:passField];
    [[self window] addSubview:getButton];

    [[self window] setBackgroundColor:[UIColor whiteColor]];
    [[self window] makeKeyAndVisible];

    return YES;
}
- (void)addTask:(id)sender
{
    NSString *t = [taskField text];

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

    [tasks addObject:t];
    [taskTable reloadData];
    [taskField setText:@""];
    [taskField resignFirstResponder];    
}
- (void)takeTask:(id)sender
{
    [tasks removeAllObjects];
    [taskTable reloadData];
    [tasks writeToFile:docPath()
            atomically:YES];
}
- (void)saveTask:(id)sender;
{
    if ([titleField text] == @""){
        //
    } else {
        NSString * original = [titleField text];
        NSString * pass = [passField text];
        NSString * step = [NSString stringWithFormat:@"%@.%@", original, pass];
        NSString * file = [NSString stringWithFormat:@"%@.plist", step];

        [tasks writeToFile:[NSString stringWithFormat:@"/tmp/%@", file] 
                atomically:YES];
        [tasks writeToFile:docPath()
                atomically:YES];
    }
}
- (void)getFile:(id)sender;
{
    NSString * original = [titleField text];
    NSString * pass = [passField text];
    NSString * step = [NSString stringWithFormat:@"%@.%@", original, pass];
    NSString * file = [NSString stringWithFormat:@"%@.plist", step];

    NSMutableArray *theTasks = [NSMutableArray arrayWithContentsOfFile:[NSString stringWithFormat:@"/tmp/%@", file]];
    tasks = [theTasks mutableCopy];

    [tasks writeToFile:docPath()
            atomically:YES];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:  (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
        [tasks removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:[NSArray   arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
}

#pragma mark - Table View management

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [tasks count];
}
- (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)applicationDidEnterBackground:(UIApplication *)application
{
    [tasks writeToFile:docPath()
            atomically:YES];
}
- (void)applicationWillTerminate:(UIApplication *)application
{
    [tasks writeToFile:docPath()
            atomically:YES];
}

@end

如果可以请帮忙谢谢。

4

1 回答 1

0

我认为你做错了。我不知道您的代码中 data.td 的用途是什么,因为您想将其保存为 username.password.plist

请尝试这个,它可能会指导您如何根据您的代码在本地保存和检索文件。

NSString *pathPlist(NSString *filename)
{
    NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                            NSUserDomainMask,
                                                            YES);
    return [[pathList objectAtIndex:0] stringByAppendingPathComponent:filename];
}

- (void)saveTask:(id)sender {
    NSString * original = @"Hello";
    NSString * pass = @"123";
    NSString * step = [NSString stringWithFormat:@"%@.%@", original, pass];
    NSString * file = [NSString stringWithFormat:@"%@.plist", step];

    NSString *path = pathPlist(file);
    NSMutableArray *array = [[NSMutableArray alloc] init];
    if (path) {
        NSArray *data = [NSArray arrayWithContentsOfFile:path];
        if (data) {
            [array setArray:data];
        }
        [array addObject:@"My Task"];
        BOOL iswritten = [array writeToFile:path atomically:YES];
        if (!iswritten) {
            NSLog(@"Failed");
        }
        [data release];
    }
}

- (void)getFile:(id)sender {
    NSString * original = @"Hello";
    NSString * pass = @"123";
    NSString * step = [NSString stringWithFormat:@"%@.%@", original, pass];
    NSString * file = [NSString stringWithFormat:@"%@.plist", step];

    NSString *path = pathPlist(file);
    NSMutableArray *array = [[NSMutableArray alloc] init];
    if (path) {
        NSArray *data = [NSArray arrayWithContentsOfFile:path];
        if (data) {
            [array setArray:data];
        }
    }
    NSLog(@"%@", array);
}

注意: var 数组保存来自文件名(username.password.plist) 的数据

于 2012-06-09T03:00:47.957 回答