user9606220
user9606220

Reputation:

Swift ios alamofire put request error Thread 1: signal SIGABRT at

I am trying to updat api from an api using alamofire http request my code is below. the api that i used https://jsonplaceholder.typicode.com/posts . i cant seem to update the data and i received and an error which is Thread 1: signal SIGABRT at at line guard let json = response.result.value as! [[String:Any]]? else{ return}

full code 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

            }
        }

    }

Upvotes: 1

Views: 548

Answers (1)

OOPer
OOPer

Reputation: 47886

The result of https://jsonplaceholder.typicode.com/posts and https://jsonplaceholder.typicode.com/posts/1 are different. The latter returns something like this:

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

It's not a JSON array but a JSON object. So your forced casting as! [[String:Any]]? fails.

Change the line to:

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

You should better not use forced casting, which makes your app crash without much useful info.

Upvotes: 0

Related Questions