Array from File in XCode

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.

  • FFmpeg for iOS5
  • layoutManager boundingRectForGlyphRange:inTextContainer: does not work for all strings
  • Capture Wi-Fi network changing event in iOS
  • How to parsing JSON object in iPhone SDK (XCode) using JSON-Framework
  • Xcode consistently fails to launch app on device: “Unknown packet reply: ”timeout“ to environment packet.”
  • How to Group by day in NSDate with NSPredicate - needed to create multiple UITableView sections
  • 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:

    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.

    Solutions Collect From Internet About “Array from File in XCode”

    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);
    }
    

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