我正在努力捕捉我的应用程序中的错误,我正在寻找使用NSError。对于如何使用它,以及如何填充它,我有点困惑。

有人能提供一个例子,我如何填充然后使用NSError?


当前回答

好吧,我通常做的是让我的方法在运行时可以error-out引用一个NSError指针。如果在该方法中确实出现了错误,我可以用错误数据填充NSError引用,并从该方法返回nil。

例子:

- (id) endWorldHunger:(id)largeAmountsOfMonies error:(NSError**)error {
    // begin feeding the world's children...
    // it's all going well until....
    if (ohNoImOutOfMonies) {
        // sad, we can't solve world hunger, but we can let people know what went wrong!
        // init dictionary to be used to populate error object
        NSMutableDictionary* details = [NSMutableDictionary dictionary];
        [details setValue:@"ran out of money" forKey:NSLocalizedDescriptionKey];
        // populate the error object with the details
        *error = [NSError errorWithDomain:@"world" code:200 userInfo:details];
        // we couldn't feed the world's children...return nil..sniffle...sniffle
        return nil;
    }
    // wohoo! We fed the world's children. The world is now in lots of debt. But who cares? 
    return YES;
}

我们可以这样使用这个方法。除非该方法返回nil,否则不要检查错误对象:

// initialize NSError object
NSError* error = nil;
// try to feed the world
id yayOrNay = [self endWorldHunger:smallAmountsOfMonies error:&error];
if (!yayOrNay) {
   // inspect error
   NSLog(@"%@", [error localizedDescription]);
}
// otherwise the world has been fed. Wow, your code must rock.

我们能够访问错误的localizedDescription,因为我们为NSLocalizedDescriptionKey设置了一个值。

了解更多信息的最好地方是苹果的文档。它真的很好。

还有一个关于可可是我女朋友的简单教程。

其他回答

extension NSError {
    static func defaultError() -> NSError {
        return NSError(domain: "com.app.error.domain", code: 0, userInfo: [NSLocalizedDescriptionKey: "Something went wrong."])
    }
}

我可以使用NSError.defaultError()每当我没有有效的错误对象。

let error = NSError.defaultError()
print(error.localizedDescription) //Something went wrong.

objective - c

NSError *err = [NSError errorWithDomain:@"some_domain"
                                   code:100
                               userInfo:@{
                                           NSLocalizedDescriptionKey:@"Something went wrong"
                               }];

斯威夫特3

let error = NSError(domain: "some_domain",
                      code: 100,
                  userInfo: [NSLocalizedDescriptionKey: "Something went wrong"])

请参考以下教程

我希望这对你有帮助,但在你必须阅读NSError的文档

这是我最近发现的一个非常有趣的链接

好吧,我通常做的是让我的方法在运行时可以error-out引用一个NSError指针。如果在该方法中确实出现了错误,我可以用错误数据填充NSError引用,并从该方法返回nil。

例子:

- (id) endWorldHunger:(id)largeAmountsOfMonies error:(NSError**)error {
    // begin feeding the world's children...
    // it's all going well until....
    if (ohNoImOutOfMonies) {
        // sad, we can't solve world hunger, but we can let people know what went wrong!
        // init dictionary to be used to populate error object
        NSMutableDictionary* details = [NSMutableDictionary dictionary];
        [details setValue:@"ran out of money" forKey:NSLocalizedDescriptionKey];
        // populate the error object with the details
        *error = [NSError errorWithDomain:@"world" code:200 userInfo:details];
        // we couldn't feed the world's children...return nil..sniffle...sniffle
        return nil;
    }
    // wohoo! We fed the world's children. The world is now in lots of debt. But who cares? 
    return YES;
}

我们可以这样使用这个方法。除非该方法返回nil,否则不要检查错误对象:

// initialize NSError object
NSError* error = nil;
// try to feed the world
id yayOrNay = [self endWorldHunger:smallAmountsOfMonies error:&error];
if (!yayOrNay) {
   // inspect error
   NSLog(@"%@", [error localizedDescription]);
}
// otherwise the world has been fed. Wow, your code must rock.

我们能够访问错误的localizedDescription,因为我们为NSLocalizedDescriptionKey设置了一个值。

了解更多信息的最好地方是苹果的文档。它真的很好。

还有一个关于可可是我女朋友的简单教程。

我将尝试总结Alex的伟大回答和jlmendezbonini的观点,添加一个修改,使所有内容都与ARC兼容(到目前为止,它不是因为ARC会抱怨,因为你应该返回id,这意味着“任何对象”,但BOOL不是对象类型)。

- (BOOL) endWorldHunger:(id)largeAmountsOfMonies error:(NSError**)error {
    // begin feeding the world's children...
    // it's all going well until....
    if (ohNoImOutOfMonies) {
        // sad, we can't solve world hunger, but we can let people know what went wrong!
        // init dictionary to be used to populate error object
        NSMutableDictionary* details = [NSMutableDictionary dictionary];
        [details setValue:@"ran out of money" forKey:NSLocalizedDescriptionKey];
        // populate the error object with the details
        if (error != NULL) {
             // populate the error object with the details
             *error = [NSError errorWithDomain:@"world" code:200 userInfo:details];
        }
        // we couldn't feed the world's children...return nil..sniffle...sniffle
        return NO;
    }
    // wohoo! We fed the world's children. The world is now in lots of debt. But who cares? 
    return YES;
}

现在,我们不再检查方法调用的返回值,而是检查error是否仍然为nil。如果不是,我们就有麻烦了。

// initialize NSError object
NSError* error = nil;
// try to feed the world
BOOL success = [self endWorldHunger:smallAmountsOfMonies error:&error];
if (!success) {
   // inspect error
   NSLog(@"%@", [error localizedDescription]);
}
// otherwise the world has been fed. Wow, your code must rock.