开关必须详尽无遗/在范围内找不到“JSON" [英] Switch must be exhaustive / Cannot find 'JSON' in scope
问题描述
伙计们!我的应用程序再次崩溃,我不知道该怎么做.有人可以帮忙吗?!顺便说一句,我是初学者..我正在学习.请客气点.
guys! Once again my app is crashing and I don't have a clue to what to do. Does anyone could help out?! By the way, I'm beginner.. I'm learning. Please, be kind.
AF.request(URL_USER_ADD, method: .post, parameters: body, encoding: JSONEncoding.default, headers: header).responseJSON { (response) in
AF.request(URL_USER_ADD, method: .post, parameters: 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)
检查错误的图像!
谢谢!
推荐答案
您的应用没有崩溃.编译失败.
Your app isn't crashing. It's failing to compile.
switch
必须详尽无遗";表示您没有处理 response.result
的所有可能情况.因为你的代码片段不包括上下文,我只能根据我自己的经验和你提供的代码猜测 response.result
是一个 Swift Result<Data,错误>
.在那种情况下,除了 .success(_)
你还必须处理 .failure(_)
"switch
must be exhaustive" indicates that you're not handling all of the possible cases of response.result
. Because your code snippet doesn't include the context, I'll just have to guess based on my own experience and the code you do provide that response.result
is a Swift Result<Data, Error>
. In that case in addition to .success(_)
you also have to handle .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
未定义的错误.事实上,我在您的屏幕截图或代码片段中没有看到它的定义,但也许它(或您打算参考的类似内容)是在我所见之外定义的.
So that's the first thing, but you also have the error that JSON
isn't defined. Indeed I don't see a definition for it in your screenshot or in the code snippet, but maybe it (or something similar that you meant to refer to) is defined outside of what I can see.
如果它在您使用的框架(或 Swift 包)中,请确保在文件顶部import
该框架.或者您打算使用 Foundation
JSON 转换工具(JSONSerialization
或 JSONDecoder
)?
If it's in a framework (or Swift Package) that you're using, make sure you import
that framework at the top of the file. Or did you intend to use Foundation
JSON conversion facilities (JSONSerialization
or JSONDecoder
)?
根据评论中的对话,我认为这就是你想要做的:
Based on conversation in comments, I think this is what you're trying to do:
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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!