Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot convert value of type 'Data' to expected argument type 'Data'

It was working fine before but somehow i am getting above error now while converting NSData to String. It showing me error on line number 2 i.e

let jsonString = NSString(data: jsonData as Data, encoding: String.Encoding.utf8.rawValue) as! String 

It shows me error.

cannot convert value of type 'Data' to expected argument type 'Data'

OR

cannot convert value of type 'NSData' to type 'Data' in coercion

do{
        jsonData = try JSONSerialization.data(withJSONObject: para, options: JSONSerialization.WritingOptions()) as NSData
        let jsonString = NSString(data: jsonData as Data, encoding: String.Encoding.utf8.rawValue) as! String
        print("json array string best json request body = \(jsonString)")
        return jsonString

    } catch _ {
        print ("UH OOO")
        return nil
    }
like image 404
Mayur Rathod Avatar asked Feb 22 '26 23:02

Mayur Rathod


1 Answers

Too much NS and too much as, just use the native Swift structs

jsonData = try JSONSerialization.data(withJSONObject: para)
let jsonString = String(data: jsonData, encoding: .utf8)
like image 74
vadian Avatar answered Feb 24 '26 13:02

vadian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!