I put "return" in quotes because I don't want to literally return it. I want to do it similar to how you pass a pointer-to-a-pointer for [NSString stringWithContentsOfFile:usedEncoding:error:]
.
I would like to make parseFiles:error
return nil and have the error reference that was passed in contain the first or second error, depending on which one failed. It seems like a Cocoa way to do it?
EDIT: Sorry, I should've been more clear about where I was having the problem. If the first path is bogus, it functions as I want. (I get the error instance outside and it prints.) If the first path is legit, as it filler string below implies, I get EXC_BAD_ACCESS
.
But now I fixed it. I need to refer to it as *error
inside the parseFiles:error:
method and use == nil
when checking if it failed. I thought I could just to if (error)
...
EDIT 2 Ok, it doesn't work. I'm getting EXC_BAD_ACCESS
. I'm not sure what I'm doing wrong with the conditions that check for the errors.
@implementation PassingError
- (id)init {
self = [super init];
NSError *error;
[self parseFiles:@"/untitled.py" error:&error];
if (error != nil) {
NSLog(@"I failed because: %@", error);
}
return self;
}
// Wraps with reading errors.
- (NSString *)parseFiles:(NSString *)path error:(NSError **)error {
NSStringEncoding enc1;
NSString *contents1 = [NSString stringWithContentsOfFile:path
usedEncoding:&enc1 error:*&error];
// there was a read error
// I need an asterisk here...
if (*error != nil) {
// ...and also one here
NSLog(@"FIRST ERROR: %@", *error);
return nil;
}
// here is where you'd do something that might cause another error,
// I'll just try and read a second file for simplicity
NSStringEncoding enc2;
NSString *contents2 = [NSString stringWithContentsOfFile:@"/untitled.py"
usedEncoding:&enc2 error:*&error];
// there was a SECOND error
if (*error != nil) {
NSLog(@"SECOND ERROR: %@", *error);
return nil;
}
// return both or whatever
return [NSArray arrayWithObjects:contents1, contents2, nil];
}
@end