Array from file in Xcode

I am creating an iOS application for task organizer. You enter a task and save it in an array. I wanted people to be able to share tasks between phones, so I added a way to save each array.

Now I use my idea locally. The page has a header and password. When the save button is pressed, the array is saved in a file (this works very well, and it saves every time), which is unique for the header and password.

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

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 

Please help if you can thank you.

0
source share
1 answer

I think you are doing it wrong. I do not know what data.td is used on your code, since you want to save it as username.password.plist

Try this and it can help you save and retrieve files locally 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); } 

Note. The var array stores data from the file name (username .password.plist)

0
source

All Articles