【问题标题】:Value of type 'Data' has no member 'writeToURL'“数据”类型的值没有成员“writeToURL”
【发布时间】:2017-01-06 10:30:11
【问题描述】:

我正在将代码转换为 swift 3 语法

if !data.writeToURL(manifestFileURL, atomically: false) {
      self.didFailWithError(WebAppError.fileSystemFailure(reason: "Could not write asset manifest to: \(manifestFileURL)", underlyingError: error))
      return
}

但我得到了错误

“数据”类型的值没有成员“writeToURL”

我已将代码转换为

if try!data.write(to: manifestFileURL, atomically: false) {
      self.didFailWithError(WebAppError.fileSystemFailure(reason: "Could not write asset manifest to: \(manifestFileURL)", underlyingError: error))
      return
}

遵循 swift 3 语法和当前方法 (https://developer.apple.com/reference/foundation/nsdata/1415134-write),但我收到错误提示 这不是函数的正确重载。请问,用swift 3写出来的正确方法是什么。任何可以引导我走向正确方向的信息将不胜感激。

谢谢

【问题讨论】:

    标签: ios swift cordova


    【解决方案1】:

    atomically: false等于无选项,可以省略参数。

    原来如此

    do {
        try data.write(to: manifestFileURL)
    } catch let error as NSError {
        self.didFailWithError(WebAppError.fileSystemFailure(reason: "Could not write asset manifest to: \(manifestFileURL)", underlyingError: error))
    }
    

    catch 子句处理错误。

    【讨论】:

      【解决方案2】:

      已重命名为:

      try! data.write(to: manifestFileURL, options: [.atomic])

      【讨论】:

      • 现在显示:'()' 不能转换为 'Bool'
      • 它不会返回Bool,现在它由错误抛出处理,只需删除你的布尔检查
      • 现在显示:调用中的额外参数“选项”
      【解决方案3】:

      使用write(to: fileURL)

      do {
       try data.write(to: manifestFileURL)
      
       } catch let error as NSError {
        print(error)
        self.didFailWithError(WebAppError.fileSystemFailure(reason: "Could not write asset manifest to: \(manifestFileURL)", underlyingError: error))}
      
        return
      }
      

      【讨论】:

      • 静态成员“原子”不能用于“NSData.WritingOtions”类型的实例
      • 现在我的回答没有显着差异了 ;-)
      • 你的答案是完美的。我错过了一些东西,这就是我更新它的原因。我也回来了:p
      猜你喜欢
      • 2017-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 2016-03-11
      • 1970-01-01
      • 1970-01-01
      • 2020-01-30
      相关资源
      最近更新 更多