localizedFailureReason returns 空

localizedFailureReason returns null

出于某种原因,我需要显示错误 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;
}

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