如何使用NSError的双重间接方法修复Clang警报?

时间:2012-06-07 15:29:51

标签: cocoa clang nserror

在使用垃圾收集的项目中: Clang 警告'潜在空取消引用....'在线分配* anError。

但根据Why does NSError need double indirection? (pointer to a pointer)

,这对我来说是正确的

任何人都可以看到我没有看到的东西吗?

- (NSData *)bookmarkDataForCurrentURL:(NSURL *)theURL error:(NSError **)anError
{
    NSArray *bkKeys = [CBPrepCommon bookmarkPropertyKeys];
    NSError *errorA = nil;
    NSData *bkmark = [theURL bookmarkDataWithOptions:NSURLBookmarkCreationPreferFileIDResolution includingResourceValuesForKeys:bkKeys relativeToURL:nil error:&errorA];

    if (![bkmark length] > 0) {
    if (errorA) {
        // error here
        *anError = [NSError errorWithDomain:[errorA domain] code:[errorA code] userInfo:[errorA userInfo]];
    }
    return nil;
}
return bkmark;

}

1 个答案:

答案 0 :(得分:0)

if (errorA && anError) {
    // error here
    *anError = [NSError errorWithDomain:[errorA domain] code:[errorA code] userInfo:[errorA userInfo]];
}

编辑:更清洁:

- (NSData *)bookmarkDataForCurrentURL:(NSURL *)theURL error:(NSError **)anError
{
    NSArray *bkKeys = [CBPrepCommon bookmarkPropertyKeys];
    NSData *bkmark = [theURL bookmarkDataWithOptions:NSURLBookmarkCreationPreferFileIDResolution includingResourceValuesForKeys:bkKeys relativeToURL:nil error:anError];

    if ([bkmark length] == 0) {
        return nil;
    }

    return bkmark;
}