无法将“数据”类型的值转换为预期的参数类型“数据”

时间:2022-03-08 16:30:49

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

它之前工作正常,但不知何故,我现在在将NSData转换为String时遇到上述错误。它显示我在第2行的错误,即

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

不能将'NSData'类型的值转换为强制类型'Data'

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
    }

1 个解决方案

#1


2  

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

太多的NS和太多,只需使用原生的Swift结构

jsonData = try JSONSerialization.data(withJSONObject: para)
let jsonString = String(data: jsonData, encoding: .utf8)

#1


2  

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

太多的NS和太多,只需使用原生的Swift结构

jsonData = try JSONSerialization.data(withJSONObject: para)
let jsonString = String(data: jsonData, encoding: .utf8)