ios – 如何使用alamofire实现put http请求

下面我有我的代码http请求获取使用此api
https://jsonplaceholder.typicode.com/posts,我想知道如何使用使用alamofire更新api上的数据,我想使用id更新它

{
    "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"
  },

我的alamofire获取请求代码

func demoApi() {
        Alamofire.request("https://jsonplaceholder.typicode.com/posts", method: .get, parameters: nil, 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)")
//                for item in json {
//
//                    if let name = item["name"] as? String {
//                        self.titleArray.append(name)
//                        print("ang name:" , name)
//
//                    }
//
//                    DispatchQueue.main.async {
//                        self.collectionView.reloadData()
//                    }
//                }
//                break

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

                    self.getAllDetail.append(item)

                    // if let title = item["title"] as? String {
                    //   self.titleArray.append(title)
                    // }

                }
                if !self.getAllDetail.isEmpty{
                    DispatchQueue.main.async {
                        self.collectionView.reloadData()
                    }
                }
                break

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

            }
        }

    }

最佳答案 你想知道,从
here获得样品

  let  params  = ["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"] as [String: Any]


Alamofire.request("https://jsonplaceholder.typicode.com/posts", 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

            }
        }

    }
点赞