2

I have a POST request that is illustrated below. I need some help on how to be able to use the response data. The response I am getting is in a form of a dictionary. What is the best practice to use here? The error message I am getting is:

Cannot assign value of type 'DataResponse' to type 'String'

func getRequest(){
    let urlString = "http://scissors.pythonanywhere.com/getRequest"
    Alamofire.request(urlString, method: .post, parameters: ["date": self.weekDays[self.itemSelectedIndex]],encoding: JSONEncoding.default, headers: nil).responseString {
        response in
        switch response.result {
        case .success:
            print(response)
            var backToString = String(data: response, encoding: String.Encoding.utf8) as String?

            break
        case .failure(let error):

            print(error)
        }
    }
}

I tried self.bookings = response as! String but it gives me a warning saying it will fail. Also tried this: Alamofire in Swift: converting response data in usable JSON dictionary with no luck.

EDIT

This is the print i get from response.

SUCCESS: {
    "10:00" =     {
        booked = false;
        name = "";
        number = "";
    };
    "10:30" =     {
        booked = false;
        name = "";
        number = "";
    };
    "11:00" =     {
        booked = false;
        name = "";
        number = "";
    };
}
EmbeddedOS
  • 173
  • 6
  • 18
  • What happens when you do `.responseJson` instead of `.responseString`? Also, what do you get when you print response.result? – LulzCow May 17 '18 at 22:26
  • Changed to .responseJson and it is looking way better thank you for that but i still need to be able to assign it to a variable i create in swfit to be able to use it in other places. Check edit from the print result @LulzCow – EmbeddedOS May 17 '18 at 23:35
  • Don't go the `responseJSON` route, it will clutter your code with optionals and casts to no end. I would stay with `responseString` and check out [this blog post](https://grokswift.com/rest-with-alamofire-swiftyjson/) for an example. Basically it boils down to use `if let value = response.result.value { print(value) }` to get your `String`. From there you should use the `Codable` protocol in order to get decent Swift-objects. If you post the JSON (the above is a linearised `NSDictionary` instead) we would be able to help you. – Patru May 18 '18 at 02:11

3 Answers3

3

I actually ended up using this method which was very simple and easy. In my case bookings was declared like this: var bookings: NSDictionary = [:]

func getRequest(){

    let urlString = "http://scissors.pythonanywhere.com/getRequest"
    Alamofire.request(urlString, method: .post, parameters: ["date": self.weekDays[self.itemSelectedIndex]],encoding: JSONEncoding.default, headers: nil).responseJSON {
        response in
        switch response.result {
        case .success(let JSON):
            self.bookings = JSON as! NSDictionary

            self.tableView.reloadData()

            break
        case .failure(let error):
            print(error)
        }
    }
}
EmbeddedOS
  • 173
  • 6
  • 18
0

Now that you have the JSON representation, you should be able to do something like this:

if let dict = response as? [String:Any]{
    if let obj = dict["10:00"] as? [String:Any]{
         let booked = obj["booked"] as? Bool ?? false
         print("booked: \(booked)");
    }
}
LulzCow
  • 1,199
  • 2
  • 9
  • 21
0

try this

import SwiftyJSON

let json = JSON(response.result.value)
ByteMe
  • 1,159
  • 2
  • 15
  • 28
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/24128705) – Akber Iqbal Sep 24 '19 at 07:21
  • Funny you say that because I had this exact same issue and solved it with those exact steps – ByteMe Sep 24 '19 at 08:17