我正在努力捕捉我的应用程序中的错误,我正在寻找使用NSError。对于如何使用它,以及如何填充它,我有点困惑。
有人能提供一个例子,我如何填充然后使用NSError?
我正在努力捕捉我的应用程序中的错误,我正在寻找使用NSError。对于如何使用它,以及如何填充它,我有点困惑。
有人能提供一个例子,我如何填充然后使用NSError?
当前回答
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"])
其他回答
答得好,亚历克斯。一个潜在的问题是NULL解引用。Apple关于创建和返回NSError对象的参考
...
[details setValue:@"ran out of money" forKey:NSLocalizedDescriptionKey];
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 nil;
...
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"])
我将尝试总结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.
好吧,我通常做的是让我的方法在运行时可以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设置了一个值。
了解更多信息的最好地方是苹果的文档。它真的很好。
还有一个关于可可是我女朋友的简单教程。
这有点超出了问题范围,但如果你没有NSError选项,你可以总是显示低级别错误:
NSLog(@"Error = %@ ",[NSString stringWithUTF8String:strerror(errno)]);