XCode中的文件数组 [英] Array from File in XCode

查看:40
本文介绍了XCode中的文件数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个任务管理器iOS应用.您键入任务并将其保存在数组中.我希望人们能够在电话之间共享任务,所以我添加了一种保存每个阵列的方法.

Am am making a task organizer iOS app. You type a task and it is saved in an array. I wanted people to be able to share the tasks between Phones so i added a way to save each Array.

现在我正在本地使用我的想法.该页面具有标题和密码.按下保存按钮后,阵列将保存到标题"和密码"唯一的文件中(此方法效果很好,并且每次都保存).

Right now I am using my idea locally. The page has a title and a password. When the save button is pressed the array is saved to a file (This works very well and it saves every time) that is unique to the Title and Password.

我需要找到一种方法,然后将文件中的所有信息返回到数组,以便可以看到它.这是我尝试过的,并且要记住,除获取任务按钮"外,其他所有功能都正常,我的问题出在 getFile void:

I need to find a way to then get all the information in the file back to the array so it can be seen. This is what i have tried and keep in mind that everything works fine except for the "get tasks button" my problem is in the getFile void:

#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

如果可以的话,请帮助.

Please Help if you can Thank You.

推荐答案

我认为您的做法不正确.我不知道您的代码上data.td的用途是什么,因为您要将其另存为username.password.plist

I think you are doing it on the wrong way. I dont know what is the used of the data.td on your code since you want to save it as username.password.plist

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

Please try this one, and it might guide you how save and retrieve files locally base on your code.

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)中的数据

Note: The var array holds the data from the filename(username.password.plist)

这篇关于XCode中的文件数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆