简体   繁体   English

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

[英]Swift 4 : Cannot convert value of type 'Data' to expected argument type 'Data'

I'm using Xcode 9, Swift 4.我正在使用 Xcode 9、Swift 4。

I'm trying to show an image in ImageView from URL using below code :我正在尝试使用以下代码从 URL 在 ImageView 中显示图像:

func getImageFromUrl(sourceUrl: String) -> UIImage {
        let url = URL(string: sourceUrl)
        let dict = NSDictionary(contentsOf: url!)
        let data = Data(dictionary: dict!)
        let image = UIImage(data: data!)
        return image
}

But I got an error in let image = UIImage(data: data!) .但是我在let image = UIImage(data: data!)出错了。

The compiler says :编译器说:

Cannot convert value of type 'Data' to expected argument type 'Data'无法将“数据”类型的值转换为预期的参数类型“数据”

What am I doing wrong?我究竟做错了什么?

/// Returns nil if image data is not correct or some network error has happened
func getImageFromUrl(sourceUrl: String) -> UIImage? {
  if let url = URL(string: sourceUrl) {
    if let imageData = try? Data(contentsOf:url) {
      return UIImage(data: imageData)
    }
  }
  return nil
}

I had a similar problem with a network call.我在网络通话中遇到了类似的问题。 I had to explicitly type Foundation.Data to make sure there wasn't some weird namespace thing going on.我必须明确键入Foundation.Data以确保没有发生一些奇怪的命名空间事情。

  func getMediaItems(completion: @escaping (Foundation.Data?, ResponseError?) -> ()) {
         let session = URLSession(configuration: .default)
         let url = self.makeURL()
         let task = session.dataTask(with: url, completionHandler: 
              { data, response, error in
                   if let imageData = data {
                        completion(data, nil) // had error here
             //etc, etc...

Pretty annoying.很烦人。 I'm not sure if I'm missing something or it's Swift 4 bug but it seemed to solve the issue.我不确定我是否遗漏了什么或者是 Swift 4 的错误,但它似乎解决了这个问题。 Without "Foundation" I got the same area at completion(data,nil) (ps. do not use this code as it's a sample and doesn't include error handling)没有“基础”,我在completion(data,nil)得到了相同的区域completion(data,nil) (ps。不要使用此代码,因为它是一个示例,不包括错误处理)

Please check any class name has Data (that you have created)in the project.请检查项目中是否有任何类名有数据(您创建的)。 Due to class name conflicts, swift may not be able to identify Foudation's Data class or your Project's Data class.由于类名冲突,swift 可能无法识别 Foudation 的 Data 类或您的 Project 的 Data 类。

暂无
暂无

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

相关问题 无法将“数据”类型的值转换为预期的参数类型“数据” - cannot convert value of type 'Data' to expected argument type 'Data' 无法将类型'(Data?)->()'的值转换为预期的参数类型'(_)->()' - Cannot convert value of type '(Data?) -> ()' to expected argument type '(_) -> ()' Swift 1.2到Swift 2:无法将类型的值转换为预期的参数类型 - Swift 1.2 to swift 2: Cannot convert value of type to expected argument type ios无法将类型“()”的值转换为预期的参数类型“字符串”,迅速3 - ios Cannot convert value of type '()' to expected argument type 'String' swift 3 无法在Swift 3中将字符串类型的值转换为预期的参数类型int - cannot convert value of type string to expected argument type int in swift 3 swift无法将类型(_,_)-> _的值转换为预期的参数类型'((_,CGFloat))-> _ - swift cannot convert value of type (_, _) -> _ to expected argument type '((_, CGFloat)) -> _ 无法将“字符串”类型的值转换为预期的参数类型“ NSManagedObject” Swift - Cannot convert value of type 'String' to expected argument type 'NSManagedObject' Swift Swift 3.0无法将(_,_)->()类型的值转换为期望的参数类型'ObjectsOrErrorBlock' - Swift 3.0 cannot convert value of type (_, _)->() to Expected argument type 'ObjectsOrErrorBlock' swift:无法将类型“()”的值转换为预期的参数类型“ [Double]” - swift : Cannot convert value of type '()' to expected argument type '[Double]' Swift 无法将类型“()”的值转换为预期的参数类型“() -> Void” - Swift Cannot convert value of type '()' to expected argument type '() -> Void'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM