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

时间:2017-01-06 10:30:12

标签: ios swift cordova

我正在将代码转换为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中写出来的正确方法是什么。任何可以引导我朝正确方向发展的信息都将不胜感激。

由于

3 个答案:

答案 0 :(得分:5)

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子句处理错误。

答案 1 :(得分:2)

它已重命名为:

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

答案 2 :(得分:2)

使用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
}