APNS : 错误域 = NSCocoaErrorDomain Code=3840

APNS :Error Domain = NSCocoaErrorDomain Code=3840

我正在向服务器注册设备数据,以获取推送通知。这是代码,

[NSURLConnection sendAsynchronousRequest: request
                                           queue: _postQueue
                               completionHandler: ^(NSURLResponse *response, NSData *responseData, NSError *connectionError) {
                                   if (connectionError) {
                                       //
                                   } else {
                                      NSError *error = nil;
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData: responseData options: NSJSONReadingMutableContainers error: &error];
                                   }
                               }];

我收到错误消息

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x17057f60 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

有人可以帮忙解决这个问题吗?

错误消息告诉您到底出了什么问题:来自您服务器的响应不包含有效 JSON。从技术上讲,JSON 必须以数组或对象(字典)开头。无论您的服务器返回什么,都不是。您可以使用 NSJSONReadingAllowFragments 选项强制消耗 JSON。

如果在使用该选项后您仍然 收到错误,那么您的服务器可能返回格式错误的 JSON(或根本没有 JSON)。你为什么不看看你服务器的日志,看看你到底发回了什么?

我遇到了同样的问题看看我在这里做了什么

我已将 json 解析方法更改为

let decodedApps = try JSONDecoder().decode(class.self, from: data!)

此处 class 包含 json 数据密钥中存在的所有密钥 现在您可以将 decodedApps 用作字典 .....其中包含键值对 可能会有帮助