简体   繁体   English

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

[英]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 . 之前它运行良好,但是现在我在将NSData to String转换NSData to String时遇到了错误。 It showing me error on line number 2 ie 它显示我在第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
    }

Too much NS and too much as , just use the native Swift structs 太多的NS和太多的as ,只是使用本机斯威夫特结构

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift 4:无法将“数据”类型的值转换为预期的参数类型“数据” - Swift 4 : Cannot convert value of type 'Data' to expected argument type 'Data' 无法将类型&#39;(Data?)-&gt;()&#39;的值转换为预期的参数类型&#39;(_)-&gt;()&#39; - Cannot convert value of type '(Data?) -> ()' to expected argument type '(_) -> ()' 无法转换类型“绑定”的值<Data Struct?> &#39;到预期的参数类型&#39;绑定<Data Struct> &#39; - Cannot convert value of type 'Binding<Data Struct?>' to expected argument type 'Binding<Data Struct>' iOS-无法将“字符串”类型的值转换为预期的参数类型“数据” - iOS-Cannot convert value of type 'String' to expected argument type 'Data' 尝试保存数组时出现错误-无法将类型[Data]的值转换为预期的参数类型&#39;[Dictionary <String, AnyObject> ]” - Trying to saveArray, got error - Cannot convert value of type '[Data]' to expected argument type '[Dictionary<String, AnyObject>]' Alamofire 的 Codables 错误“无法将 '[String: Any]' 类型的值转换为预期的参数类型 'Data'” - Codables error with Alamofire “Cannot convert value of type '[String : Any]' to expected argument type 'Data'” 无法转换“字典”类型的值 <String, Any> ? 到预期的参数类型“数据” - Cannot convert value of type 'Dictionary<String, Any>?' to expected argument type 'Data' 无法将类型&#39;()&#39;的值转换为预期的参数&#39;()-&gt; void&#39; - cannot convert value of type '()' to expected argument '() -> void' 无法将类型的值转换为预期的参数Firebase - Cannot convert value of type to expected argument Firebase 无法转换期望参数类型GTLServiceCompletionHandler的值 - Cannot convert value of expected argument type GTLServiceCompletionHandler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM