2

我将“return”放在引号中,因为我不想从字面上返回它。我想这样做类似于您将指针传递给[NSString stringWithContentsOfFile:usedEncoding:error:].

我想让parseFiles:errorreturn nil 并让传入的错误引用包含第一个或第二个错误,具体取决于哪个错误。这似乎是一种可可的方式来做到这一点?

编辑:对不起,我应该更清楚我在哪里遇到问题。如果第一条路径是虚假的,它会按我的意愿运行。(我在外面得到错误实例并打印出来。)如果第一个路径是合法的,正如下面的填充字符串所暗示的那样,我得到EXC_BAD_ACCESS.

但现在我修好了。我需要在方法*error内部引用它,并在检查它是否失败时使用。我以为我可以...parseFiles:error:== nilif (error)

编辑 2好的,它不起作用。我得到EXC_BAD_ACCESS。我不确定我在检查错误的条件下做错了什么。

@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
4

1 回答 1

3

在 Objective-C 中传递指针会让人感到困惑。我记得很难掌握需要做的事情。当你有这样的方法时:

- (BOOL) saveValuesAndReturnError:(NSError **) error
{
    BOOL success = [self doSomethingImportant];
    
    if (!success && error)
    {
        // Unsuccessful and error is a valid ptr-to-ptr-to-NSError.
        
        // Basically, someone has given us the address of a (NSError *).
        // We can modify what that pointer points to here.
        *error = [NSError errorWithDomain:@"myDomain" code:100 userInfo:nil];
    }
    
    return success;
}

这旨在像这样调用:

// If the caller doesn't care that it failed:
[someObject saveValuesAndReturnError:NULL];



// Or, if the caller wants to get error information on failure
NSError *anError = nil;
BOOL success;

// pass address of our (NSError *)
success = [someObject saveValuesAndReturnError:&anError];
if (!success)
{
    // anError now points to an NSError object, despite being initialised to nil,
    // because we passed the address of our NSError ptr, the method was able to 
    // change where `anError` points to.
    NSLog (@"An error occurred while saving values: %@", anError);
}

在这种情况下,可能非常相关的阅读内容是CIMGF 博客文章,该文章恰好涵盖了该主题。

然而...

我记得前段时间读过那些通过方法参数返回错误的方法,例如stringWithContentsOfFile:usedEncoding:error:不保证不会修改错误参数以获得成功。换句话说,如果方法成功,您不能依赖错误参数的值。在您的特定情况下,最好这样做:


- (NSString *)parseFiles:(NSString *)path error:(NSError **)error {

    NSStringEncoding enc1, enc2;
    NSError *innerError;
    NSString *contents1 = [NSString stringWithContentsOfFile:path
                                                usedEncoding:&enc1
                                                       error:&innerError];
    
    if (contents1 == nil)
    {
        if (error) *error = innerError;
        return nil;
    }

    NSString *contents2 = [NSString stringWithContentsOfFile:@"/untitled.py"
                                                usedEncoding:&enc2
                                                       error:&innerError];
    
    if (contents2 == nil)
    {
        if (error) *error = innerError;
        return nil;
    }
    
    // do whatever with contents1 and contents2
}

于 2010-05-25T21:51:38.403 回答