ios - 无法将类型 'Data' 的值转换为预期的参数类型 'Data'

标签 ios iphone swift3 xcode8 nsmutabledictionary

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

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

它向我显示错误。

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

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
    }

最佳答案

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

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

关于ios - 无法将类型 'Data' 的值转换为预期的参数类型 'Data',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41670110/

相关文章:

ios - Xcode8 苹果推送通知服务有问题

ios - 如何在以编程方式创建标签栏 Controller 后添加导航界面(Swift)

objective-c - 仅 iPhone 应用程序在 iPad 上运行时 UIBarButtonItem 纹理偏移

ios - 无法在 Swift 上设置/获取映射对象数组到 userDefaults

iphone - 将图像保存到自定义库

ios - (Swift 3)我需要向用户发送通知以在特定时间打开应用程序。使用本地通知还是推送通知更好?

ios - 如何在xib中设置正确的位置

iphone - 如何将 appDelegate 绑定(bind)到应用程序的其余部分?

ios - 命令因信号 : Segmentation fault: 11 while archiving 而失败

ios - Swift 3 计数计时器未能在 UILabel 中显示更新的数字