我正在尝试使用alamofire http请求从api更新api,我的代码如下。我使用的apihttps://jsonplaceholder.typicode.com/posts。我似乎无法更新数据和我收到的一个错误是线程1:signal SIGABRT at at line guard let json=response.result.value as![[String:Any]]?否则{返回}
完整代码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 consequeuntur expedita et cum\nreprenderit molestiae ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto“]作为[字符串:任意]
        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对象。所以你的强制施法失败了。
将行更改为:
    guard let json = response.result.value as? [String:Any] else {
        return //<- Put breakpoint here, when you find unexpected early return
    }

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

10-05 22:16