I have an api call which its response is a data including a bool. it is NOT a JSON object so JSONDecoder fails to decode it. it's some bytes that needs to be converted to a Bool.
respose in postman: true or false
what is requested :
let dataTask = session.dataTask(with: urlRequest) { responseData, urlResponse, err -> Void in
let data: Data = responseData
}
thanks for your help in advance
This function should do it:
func parse(data: Data) -> Bool? {
return String(data: data, encoding: .utf8).flatMap(Bool.init)
}
Try for yourself:
let responseStrings = ["false", "true", "bogus"]
let responseBodies = responseStrings.map { Data($0.utf8) }
responseBodies.map(parse) // => [false, true, nil]