【问题标题】:localizedFailureReason returns null本地化失败原因返回 null
【发布时间】:2023-03-10 08:47:01
【问题描述】:

出于某种原因,我需要显示错误本地化失败原因。我收到 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;
}

【问题讨论】:

    标签: ios nserror


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2017-03-23
      • 1970-01-01
      • 2020-09-18
      • 2019-12-26
      • 2012-10-26
      • 2021-12-09
      • 1970-01-01
      • 2012-11-03
      • 2013-06-26
      相关资源
      最近更新 更多