如何从NSError获取更多有用信息?

时间:2021-11-06 22:05:19

I want to get some useful information from NSError. If I print out [error userInfo], I get the following:

我想从NSError获得一些有用的信息。如果我打印出[错误userInfo],我得到以下内容:

{
    NSFilePath = "/Users/apple/Library/Application Support/iPhone Simulator/5.1/Applications/08260B6A-4D65-48DF-ADD1-FFC8750081E8/Documents/abc";
    NSUnderlyingError = "Error Domain=NSPOSIXErrorDomain Code=17 \"The operation couldn\U2019t be completed. File exists\"";
}

I want to show the last line : "File exists", but how can i pick it out?

我想显示最后一行:“文件存在”,但我怎么能把它拿出来?

I tried:

localizedDescription
localizedFailureReason
localizedRecoverySuggestion
localizedRecoveryOptions
recoveryAttempter

Non of them show "File exists".

其中没有显示“文件存在”。

4 个解决方案

#1


24  

Finally, I follow code for perfect NSError print. Thanks @jbat100 and @Peter Warbo, I add a little bit on them code:

最后,我按照代码进行完美的NSError打印。谢谢@ jbat100和@Peter Warbo,我在它们上添加了一些代码:

    NSDictionary *userInfo = [error userInfo];
    NSString *errorString = [[userInfo objectForKey:NSUnderlyingErrorKey] localizedDescription];

#2


1  

How about:

NSDictionary *userInfo = [error userInfo];
NSString *error = [userInfo objectForKey:@"NSUnderlyingError"];
NSLog(@"The error is: %@", error);

#3


1  

If you look up the NSError documentation, it has a User info dictionary keys section which has a constant defined as NSUnderlyingErrorKey (it also has a description for the keys).

如果您查找NSError文档,它有一个用户信息字典键部分,其常量定义为NSUnderlyingErrorKey(它还有一个键的描述)。

NSDictionary *userInfo = [error userInfo];
NSError *underlyingError = [userInfo objectForKey:NSUnderlyingErrorKey];
NSString *underlyingErrorDescription = [underlyingError localizedDescription];

#4


1  

localizedRecoverySuggestion is very useful. You can get the JSON string from it:

localizedRecoverySuggestion非常有用。您可以从中获取JSON字符串:

NSString *JSON = [[error userInfo] valueForKey:NSLocalizedRecoverySuggestionErrorKey] ;

            NSError *aerror = nil;
            NSDictionary *json = [NSJSONSerialization JSONObjectWithData: [JSON dataUsingEncoding:NSUTF8StringEncoding]
                                                                 options: NSJSONReadingMutableContainers
                                                                   error: &aerror];

#1


24  

Finally, I follow code for perfect NSError print. Thanks @jbat100 and @Peter Warbo, I add a little bit on them code:

最后,我按照代码进行完美的NSError打印。谢谢@ jbat100和@Peter Warbo,我在它们上添加了一些代码:

    NSDictionary *userInfo = [error userInfo];
    NSString *errorString = [[userInfo objectForKey:NSUnderlyingErrorKey] localizedDescription];

#2


1  

How about:

NSDictionary *userInfo = [error userInfo];
NSString *error = [userInfo objectForKey:@"NSUnderlyingError"];
NSLog(@"The error is: %@", error);

#3


1  

If you look up the NSError documentation, it has a User info dictionary keys section which has a constant defined as NSUnderlyingErrorKey (it also has a description for the keys).

如果您查找NSError文档,它有一个用户信息字典键部分,其常量定义为NSUnderlyingErrorKey(它还有一个键的描述)。

NSDictionary *userInfo = [error userInfo];
NSError *underlyingError = [userInfo objectForKey:NSUnderlyingErrorKey];
NSString *underlyingErrorDescription = [underlyingError localizedDescription];

#4


1  

localizedRecoverySuggestion is very useful. You can get the JSON string from it:

localizedRecoverySuggestion非常有用。您可以从中获取JSON字符串:

NSString *JSON = [[error userInfo] valueForKey:NSLocalizedRecoverySuggestionErrorKey] ;

            NSError *aerror = nil;
            NSDictionary *json = [NSJSONSerialization JSONObjectWithData: [JSON dataUsingEncoding:NSUTF8StringEncoding]
                                                                 options: NSJSONReadingMutableContainers
                                                                   error: &aerror];