gpt4 book ai didi

ios - Swift ios alamofire put请求错误Thread 1 : signal SIGABRT at

转载 作者:行者123 更新时间:2023-11-28 07:50:55 25 4
gpt4 key购买 nike

我正在尝试使用 alamofire http 请求从 api 更新 api 我的代码如下。我使用的 API https://jsonplaceholder.typicode.com/posts .我似乎无法更新数据,我收到了一个错误,即线程 1:信号 SIGABRT at at line guard let json = response.result.value as! [[字符串:任何]]?否则{返回}

完整代码 func updateApi(){

        let  params  = ["userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit r

ecusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"] as [String: Any]

        Alamofire.request("https://jsonplaceholder.typicode.com/posts/1", method: .put, parameters: params, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in

switch(response.result) {


case .success(_):
guard let json = response.result.value as! [[String:Any]]? else{ return}
print("Response \(json)")


break

case .failure(_):
print("Error")
break

}
}

}

最佳答案

https://jsonplaceholder.typicode.com/postshttps://jsonplaceholder.typicode.com/posts/1的结果是不一样的。后者返回如下内容:

{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}

它不是一个 JSON 数组,而是一个 JSON 对象。所以你强制类型转换 as! [[String:Any]]? 失败。

将行更改为:

    guard let json = response.result.value as? [String:Any] else {
return //<- Put breakpoint here, when you find unexpected early return
}

您最好不要使用强制转换,这会使您的应用在没有太多有用信息的情况下崩溃。

关于ios - Swift ios alamofire put请求错误Thread 1 : signal SIGABRT at,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49724619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com