Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nashyo

macrumors 6502
Original poster
Oct 1, 2010
299
0
Bristol
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)

16homyv.png



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.
 
Last edited:
I'll remind you again:

Asking For Help 101 - non-generic thread titles. Please be courteous of those you seek help from and use a thread title that describes the issue to some extent and not just something along the lines of "I need help". Thank you for your consideration.

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.
 
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.)
 
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.)

I don't see the extra '!'
 
I'll do it for you, Art.

Code:
- (IBAction)showResults:(id)sender {
    NSString *docDir = [NSSearchPathForDirectoriesInDomains(
                                                            NSDocumentDirectory, 
                                                            NSUserDomainMask, YES) 
                        objectAtIndex:0];
    
    NSString *surveyFile = [docDir stringByAppendingPathComponent:@"surveyresults.csv"];
    
    [B]if ([COLOR="Red"]![/COLOR][[NSFileManager defaultManager] fileExistsAtPath:surveyFile]) {[/B]
        NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:surveyFile];
        NSString *surveyResults = [[NSString alloc] initWithData:[fileHandle availableData] encoding:NSUTF8StringEncoding];
        
        [fileHandle closeFile];
        self.resultsView.text = surveyResults;
    }
}
 
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
 
There is only one '!' which means to me if this file does not exist then do the following.

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?
 
By the way this code is exactly as it appears in a book called 'sams teach yourself iPhone development in 24 hours.

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
 
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?

Ah, simple. Thanks
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.