File Management Basics in IOS

Discussion in 'iOS Programming' started by nashyo, Feb 1, 2012.

  1. nashyo, Feb 1, 2012
    Last edited: Feb 1, 2012

    nashyo macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #1
    Preliminary notes:
    I am using Xcode 4.2.1
    I am working with IOS 5 SDK
    I have enables ARC
    I am using storyboard
    I had no warning in my code
    I am running the following in the iPhone simulator
    The csv file that is generated can be found within the finder, and stores data correctly
    These methods are implemented in the controller of the view containing the buttons and text inputs (as seen in pic)

    [​IMG]


    Code:
    - (IBAction)storeResults:(id)sender {
        NSString *csvLine = [NSString stringWithFormat:@"%@,%@,%@\n",
                             self.firstName.text,
                             self.lastName.text,
                             self.email.text];
        NSString *docDir = [NSSearchPathForDirectoriesInDomains(
                                                NSDocumentDirectory, 
                                                NSUserDomainMask, YES) 
                            objectAtIndex:0];
    
        NSString *surveyFile = [docDir stringByAppendingPathComponent:@"surveyresults.csv"];
        
        if (![[NSFileManager defaultManager] fileExistsAtPath:surveyFile]) {
            [[NSFileManager defaultManager] createFileAtPath:surveyFile contents:nil attributes:nil];
        }
        
        NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:surveyFile];
        [fileHandle seekToEndOfFile];
        [fileHandle writeData:[csvLine dataUsingEncoding:NSUTF8StringEncoding]];
        [fileHandle closeFile];
        
        self.firstName.text = @"";
        self.lastName.text =@"";
        self.email.text = @"";
    }
    Code:
    - (IBAction)showResults:(id)sender {
        NSString *docDir = [NSSearchPathForDirectoriesInDomains(
                                                                NSDocumentDirectory, 
                                                                NSUserDomainMask, YES) 
                            objectAtIndex:0];
        
        NSString *surveyFile = [docDir stringByAppendingPathComponent:@"surveyresults.csv"];
        
        if (![[NSFileManager defaultManager] fileExistsAtPath:surveyFile]) {
            NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:surveyFile];
            NSString *surveyResults = [[NSString alloc] initWithData:[fileHandle availableData] encoding:NSUTF8StringEncoding];
            
            [fileHandle closeFile];
            self.resultsView.text = surveyResults;
        }
    }
    The results won't show in the results text field. :mad:

    I'm confident all of my connections are set up correctly, and I have the properties set as public.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    I'll remind you again:

    Please don't make me have to do it anymore.

    And, yes, you can edit the title of the thread after it's been posted.
     
  3. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
  4. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    Unless I'm misreading your code, it looks like it only will attempt to display the results if there wasn't anything at the proper directory. In other words, you have an extra ! in your if condition (I'd quote and bold the mistake but it's a bit tedious on the iPhone.)
     
  5. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #5
    I don't see the extra '!'
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    I'll do it for you, Art.

     
  7. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #7
    There is only one '!' which means to me if this file does not exist then do the following.

    By the way this code is exactly as it appears in a book called 'sams teach yourself iPhone development in 24 hours.

    Ye...not a great book
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    So, if the file doesn't exist, you should read it in and update the textView. Conversely, if the file does exist, don't read it in and don't update the textView. Does that make sense to you?
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    1. Locate the website for the book.
    2. Go to the website.
    3. Look for a list of errors in the book, usually called "errata".
    4. While there, look for a download that contains all the source code from the book, because sometimes the actual source code is correct but the printer broke it.

    For #1, look in the book itself, usually in the Foreward, Introduction, or something before Chapter 1. Also look in any appendices, end-notes, etc.

    Suggestions for google search terms:
    errata sams teach yourself iPhone development in 24 hours
    download sams teach yourself iPhone development in 24 hours
    publisher sams teach yourself iPhone development in 24 hours
     
  10. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #10
    Ah, simple. Thanks
     
  11. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #11
    I recommend that you don't use NSFileHandle. AT ALL. FOR ANYTHING.

    It has a nasty habit of throwing exceptions for errors.

    NSString and NSData have handy methods for writing themselves out and reading themselves in, which are more convenient and report errors better than NSFileHandle.
     

Share This Page