views:

1301

answers:

1

I am just curious if I am doing this right.

NSString *fileContents;    
NSError *fileError = nil;

fileContents = [[NSString stringWithContentsOfFile:fileOnDisk
                          encoding:NSMacOSRomanStringEncoding
                          error:&fileError] retain];

if(fileError != nil) {
    NSLog(@"Error : %@", [fileError localizedDescription]);
}

// Other Code ...
[fileContents release];

.

EDIT (to reflect bbums comments)

.

NSString *fileOnDisk = @"/Users/Gary/Documents/Xcode/RnD/Maya.MEL";
NSError  *fileError; // Should this be *fileError = nil;
NSString *fileContents;
int       status = 0;

fileContents = [[NSString stringWithContentsOfFile:fileOnDisk
                          encoding:NSMacOSRomanStringEncoding
                          error:&fileError] retain];

if(fileContents == nil) {
    NSLog(@"FileError: %@", [fileError localizedDescription]);
    status = 1;
} else {
    NSLog(@"Success  : %@", fileContents);
}

// Clean up
[fileContents release];
[pool drain];
return status;

gary

+28  A: 
NSError *fileError = nil;
....
if(fileError != nil)
....

That is incorrect. You must not assume anything about the return-by-reference value of fileError until you check whether or not fileContents was nil. Not ever. Setting fileError to nil prior to calling the pass-error-by-reference method does nothing useful.

That is, your code should read (fixed now that I'm no longer running from plane to plane and hopping on WiFi in between connections...):

NSString *fileContents;    
NSError *fileError;

fileContents = [[NSString stringWithContentsOfFile:fileOnDisk
                          encoding:NSMacOSRomanStringEncoding
                          error:&fileError] retain];

if(fileContents == nil) {
    NSLog(@"Error : %@", [fileError localizedDescription]);
    // ... i.e. handle the error here more
    return ...; // often returning after handling the errors, sometimes you might continue
}

// Other Code ...
[fileContents release];
bbum
bbum, did you mean `if(fileContents == nil) {` ?
Todd Ditchendorf
This is described in Apple's documentation here: http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/ErrorHandlingCocoa/CreateCustomizeNSError/CreateCustomizeNSError.html#//apple_ref/doc/uid/TP40001806-CH204-SW2
Johan Kool
Thanks bbum, I can see where your going there, and I can see how it now makes more sense to check the fileContents for nil. However, is it not the same difference, don't you only get an error if fileContents is nil. Unless of course there are situations where fileContents is not nil and their is an error. Thanks for the tip.
fuzzygoat
Also in your new version do you still need to set NSError *fileError = nil; it seems a bit pointless as your now checking fileContents? COuld you have NSError *fileError; instead?
fuzzygoat
Edited to fix my rushed answer. Sorry about that. Yes -- no need to initialize fileError because you *never* read it *unless* you first check the return value of the method to determine that an error actually happened.
bbum
fuzzygoat