【问题标题】:Switch must be exhaustive / Cannot find 'JSON' in scope开关必须详尽/在范围内找不到“JSON”
【发布时间】:2021-06-28 22:50:26
【问题描述】:

伙计们! 我的应用程序再次崩溃,我不知道该怎么做。有人可以帮忙吗?! 顺便说一句,我是初学者..我正在学习。请善待。

AF.request(URL_USER_ADD, 方法: .post, 参数: body, encoding: JSONEncoding.default, headers: header).responseJSON { (response) in

        switch response.result {
              case .success(let result):
                 if let json = result as? Data {
                    guard let data = response.data else { return }
                    let json = JSON(data: data)
                    let id = json["_id"].stringValue
                    let color = json["avatarColor"].stringValue
                    let avatarName = json["avatarName"].stringValue
                    let email = json["email"].stringValue
                    let name = json["name"].stringValue
                    
                    UserDataService.instance.setUserData(id: id, color: color, avatarName: avatarName, email: email, name: name)
                    completion(true)
                 
                 } else {
                    completion(false)
                    debugPrint(response.result as Any)

检查错误的图像!

谢谢!

enter image description here

【问题讨论】:

  • 添加案例.failure
  • 你的应用没有崩溃,你的应用没有编译。

标签: ios json swift alamofire xcode12


【解决方案1】:

您的应用没有崩溃。编译失败。

switch必须是详尽的”表示您没有处理response.result的所有可能情况。因为您的代码 sn-p 不包含上下文,所以我只需要根据我自己的经验猜测您提供的代码 response.result 是 Swift Result<Data, Error>。在这种情况下,除了.success(_),您还必须处理.failure(_)

switch response.result
{
    case .success(let result):
        // The code you already have - more on that in a bit

    // The thing that's missing
    case .failure (let error):
        // Do something with error here that makes sense for your app
}

这是第一件事,但您也遇到了 JSON 未定义的错误。实际上,我在您的屏幕截图或代码 sn-p 中没有看到它的定义,但也许它(或您想要引用的类似内容)是在我能看到的范围之外定义的。

如果它位于您正在使用的框架(或 Swift 包)中,请确保您在文件顶部 import 那个框架。或者您是否打算使用Foundation JSON 转换工具(JSONSerializationJSONDecoder)?

附录

根据 cmets 中的对话,我认为这就是你想要做的:

switch response.result
{
    case .success(let result):
        guard let json = try? JSON(data: result) else { fallthrough }
        let id = json["_id"].stringValue
        let color = json["avatarColor"].stringValue
        let avatarName = json["avatarName"].stringValue
        let email = json["email"].stringValue
        let name = json["name"].stringValue

        UserDataService.instance.setUserData(id: id, color: color, avatarName: avatarName, email: email, name: name)
        completion(true)

    case .failure(let error)
         completion(false)
         debugPrint(response.result as Any)
}

【讨论】:

  • 知道了。所以切换必须不仅仅是一个失败或成功,它必须两者兼而有之。好的!当我在看在线课程时,老师说我们数据,因为 JSON 所做的是它从响应数据中创建一个对象。让 json = JSON(data: data) JSON 假设是一个初始化器并使用'import SwiftyJSON'但仍然失败。
  • 一般来说,switch 必须处理所有可能的情况,无论它打开什么。对于enum 类型,即Result,这通常意味着显式处理每个案例,但对于像String 这样的一些事情,您通常必须包含default 案例。
  • 它是否因与以前相同的错误而失败?还是有什么不同?
  • no不一样,上面写着:let json = JSON(data: data) XCall可以抛出,但是没有标记'try',错误不处理
  • 我很震惊..哈哈没有错误!有效!非常感谢您。这是一个很好的教训!
【解决方案2】:

第一个截图是教师代码: enter image description here

第二个是我的,因为我拒绝将错误作为响应的属性: enter image description here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 2022-01-07
    • 2020-11-23
    • 2021-06-18
    • 2021-01-15
    相关资源
    最近更新 更多