“URL”类型的值没有成员“URLByAppendingPathComponent”

Adi*_*dim 2 ios cordova swift

我正在开发一个cordova应用程序并尝试将它生成的swift代码转换为swift 3语法,因为当我尝试构建它时它会产生错误。我有这个功能

init(configuration: WebAppConfiguration, versionsDirectoryURL: URL, initialAssetBundle: AssetBundle) {
  self.configuration = configuration
  self.versionsDirectoryURL = versionsDirectoryURL
  self.initialAssetBundle = initialAssetBundle

  downloadDirectoryURL = versionsDirectoryURL.appendingPathComponent("Downloading")

  queue = DispatchQueue(label: "com.meteor.webapp.AssetBundleManager", attributes: [])

  downloadedAssetBundlesByVersion = [String: AssetBundle]()
  loadDownloadedAssetBundles()

  let operationQueue = OperationQueue()
  operationQueue.maxConcurrentOperationCount = 1
  operationQueue.underlyingQueue = queue

  // We use a separate to download the manifest, so we can use caching
  // (which we disable for the session we use to download the other files 
  // in AssetBundleDownloader)
  session = URLSession(configuration: URLSessionConfiguration.default, delegate: nil, delegateQueue: operationQueue)
}
Run Code Online (Sandbox Code Playgroud)

6号线

downloadDirectoryURL = versionsDirectoryURL.appendingPathComponent("Downloading")
Run Code Online (Sandbox Code Playgroud)

抛出错误

“URL”类型的值没有成员“URLByAppendingPathComponent”

我不明白是什么导致了这个错误,我已经浏览了 swift 3 文档以及其他在线答案,但该行应该没有错误,请对解决此错误的任何见解将不胜感激

小智 5

“URL”类型有一个“appendingPathComponent”方法

确保您没有犯我为 [URL] 类型的变量调用方法的错误

let documentURL = FileManager.default.urls(for: .documentDirectory, in: 
.userDomainMask)
        let fileURL = 
documentURL.appendingPathComponent("tempImage.jpg")
Run Code Online (Sandbox Code Playgroud)

通过确保它是 URL 类型的变量来修复

let documentURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileURL = documentURL.appendingPathComponent("tempImage.jpg")
Run Code Online (Sandbox Code Playgroud)