我正在制作一个任务管理器 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
如果可以请帮忙谢谢。