localizedFailureReason返回null

时间:2015-03-17 12:47:07

标签: ios nserror

出于某种原因,我需要显示错误的localizedFailureReason。我收到了NSError

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Unexpected end of file while parsing object.) UserInfo=0x178a72b80 {NSDebugDescription=Unexpected end of file while parsing object.}

当我尝试记录

[error localizedFailureReason]

我为此获得空值。我可以知道是什么造成的吗?我只是

The operation couldn’t be completed. (Cocoa error 3840.)

代表

[error localizedDescription]  only

以下是该方法的完整代码:

+(NSDictionary *)insertErrorIntoFormat:(NSError*)error{

NSLog(@"1.%@",[error localizedDescription]);
NSLog(@"2.%@",[error description]);
NSLog(@"3.%@",[self getCurrentDateTimeString]);

NSDictionary *threat = [NSDictionary dictionaryWithObjectsAndKeys:
                        [self getCurrentDateTimeString], kTimestamp,
                        [error localizedDescription], @"logs",
                        [error localizedFailureReason], @"reason",
                        nil];


   return threat;
}

1 个答案:

答案 0 :(得分:0)

NSError不需要具有失败原因字符串。返回nil完全有效。在将其添加到NSDictionary或NSArray之前,需要检查返回值,因为这些数据结构不能包含nil值。