我正在解决我的应用程序中的错误,考虑使用NSError
。但我对如何使用它以及如何填充它有些困惑。
请问是否能提供一个示例,展示如何填充并使用NSError
?
我正在解决我的应用程序中的错误,考虑使用NSError
。但我对如何使用它以及如何填充它有些困惑。
请问是否能提供一个示例,展示如何填充并使用NSError
?
好的,通常我会使可能在运行时出现错误的方法接受一个指向 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;
}
// 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.
根据我最近的实现,我想提供一些更多的建议。我研究了Apple的一些代码,我认为我的代码行为方式基本一致。
上面的帖子已经解释了如何创建NSError对象并返回它们,所以我不会在这个部分浪费时间。我只是尝试建议一种将错误(代码、消息)集成到您自己的应用程序中的好方法。
我建议创建一个头文件,该头文件将概述您域中所有错误(即应用程序、库等)。我的当前头文件看起来像这样:
FSError.h
FOUNDATION_EXPORT NSString *const FSMyAppErrorDomain;
enum {
FSUserNotLoggedInError = 1000,
FSUserLogoutFailedError,
FSProfileParsingFailedError,
FSProfileBadLoginError,
FSFNIDParsingFailedError,
};
FSError.m
#import "FSError.h"
NSString *const FSMyAppErrorDomain = @"com.felis.myapp";
+ (FSProfileInfo *)profileInfoWithData:(NSData *)data error:(NSError **)error
{
FSProfileInfo *profileInfo = [[FSProfileInfo alloc] init];
if (profileInfo)
{
/* ... lots of parsing code here ... */
if (profileInfo.username == nil)
{
*error = [NSError errorWithDomain:FSMyAppErrorDomain code:FSProfileParsingFailedError userInfo:nil];
return nil;
}
}
return profileInfo;
}
error.localizedDescription
)如下所示:
Error Domain=com.felis.myapp Code=1002 "无法完成操作。(com.felis.myapp错误1002。)"
1002
的含义,因此现在我们需要为每个代码实现一些好的消息。strings
文件。字符串文件很容易本地化。该文件可能看起来像下面这样:
FSError.strings
"1000" = "User not logged in.";
"1001" = "Logout failed.";
"1002" = "Parser failed.";
"1003" = "Incorrect username or password.";
"1004" = "Failed to parse FNID."
2) 添加宏,将整数代码转换为本地化错误消息。我在我的Constants+Macros.h文件中使用了2个宏。为方便起见,我总是将此文件包含在前缀头文件 (MyApp-Prefix.pch
) 中。
Constants+Macros.h
// error handling ...
#define FS_ERROR_KEY(code) [NSString stringWithFormat:@"%d", code]
#define FS_ERROR_LOCALIZED_DESCRIPTION(code) NSLocalizedStringFromTable(FS_ERROR_KEY(code), @"FSError", nil)
3) 现在基于错误代码展示用户友好的错误信息变得更加容易,例如:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:FS_ERROR_LOCALIZED_DESCRIPTION(error.code)
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
.pch
文件)中导入此文件,以便在任何地方都可以使用。如果您的意思是您只使用了其中的1个宏,那可能是可行的。也许将int
转换为NSString
并不是真正必要的,尽管我还没有测试过。 - Wolfgang Schreurs.strings
文件)中,因为这是苹果宏查找的位置。在这里阅读有关使用 NSLocalizedStringFromTable
的信息:https://developer.apple.com/library/mac/documentation/cocoa/conceptual/loadingresources/Strings/Strings.html - Wolfgang SchreursFS_ERROR_LOCALIZED_DESCRIPTION
中的代码检查名为FSError.strings
的文件中的可本地化字符串。如果您对此不熟悉,您可能需要查看苹果的本地化指南.strings
文件。 - Wolfgang Schreurs亚历克斯,你的回答很好。潜在的问题是空指针引用。苹果关于创建和返回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"
}];
Swift 3
let error = NSError(domain: "some_domain",
code: 100,
userInfo: [NSLocalizedDescriptionKey: "Something went wrong"])
我见过的另一种设计模式涉及使用块,特别是在方法异步运行时非常有用。
假设我们已经定义了以下错误代码:
typedef NS_ENUM(NSInteger, MyErrorCodes) {
MyErrorCodesEmptyString = 500,
MyErrorCodesInvalidURL,
MyErrorCodesUnableToReachHost,
};
- (void)getContentsOfURL:(NSString *)path success:(void(^)(NSString *html))success failure:(void(^)(NSError *error))failure {
if (path.length == 0) {
if (failure) {
failure([NSError errorWithDomain:@"com.example" code:MyErrorCodesEmptyString userInfo:nil]);
}
return;
}
NSString *htmlContents = @"";
// Exercise for the reader: get the contents at that URL or raise another error.
if (success) {
success(htmlContents);
}
}
然后,当您调用它时,您不需要担心声明NSError对象(代码补全将为您完成),也不需要检查返回值。您只需提供两个块:一个在发生异常时被调用,另一个在成功时被调用:
[self getContentsOfURL:@"http://google.com" success:^(NSString *html) {
NSLog(@"Contents: %@", html);
} failure:^(NSError *error) {
NSLog(@"Failed to get contents: %@", error);
if (error.code == MyErrorCodesEmptyString) { // make sure to check the domain too
NSLog(@"You must provide a non-empty string");
}
}];
我将尝试总结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.
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.
虽然有点超出问题范围,但如果您没有NSError选项,您总是可以显示低级错误:
NSLog(@"Error = %@ ",[NSString stringWithUTF8String:strerror(errno)]);
id
转换为BOOL
时存在一些问题。如果有任何轻微的ARC兼容变化,将不胜感激。 - NSTJBOOL
。如果出现错误,请返回NO
,而不是检查返回值,只需检查error
。如果为nil
,则继续执行,如果!= nil
,则处理它。 - Gabriele Petronella**error
不是 nil。否则程序会抛出一个完全不友好并且不明确发生了什么的错误。 - FreeAsInBeer