Adim
Adim

Reputation: 811

Value of type 'URL' has no member 'URLByAppendingPathComponent'

I am working on a cordova app and trying to convert the swift code it generated to swift 3 syntax because it is producing errors when I try to build it. I have this function

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)
}

Line 6

downloadDirectoryURL = versionsDirectoryURL.appendingPathComponent("Downloading")

is throwing an error

Value of type 'URL' has no member 'URLByAppendingPathComponent'

and I do not understand what is causing this error, I have looked through swift 3 documentation as well as other answers online, but the line is supposed to be error free, please any insight into resolving this error would be greatly appreciated

Upvotes: 2

Views: 6892

Answers (3)

user11182243
user11182243

Reputation:

The correct syntax swift 4+ is:

        let VideoFilePath = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("mergeVideo\(arc4random()%1000)d")?.appendingPathExtension("mp4").absoluteString

Upvotes: 0

Edwin R
Edwin R

Reputation: 61

There is an 'appendingPathComponent' method for type 'URL'

Make sure you didn't do the mistake I did of calling the method for a variable of type [URL]

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

fixed by making sure it's a variable of type URL

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

Upvotes: 5

Tj3n
Tj3n

Reputation: 9933

I think it's appendPathComponent not appendingPathComponent, simply retype it will suggest you the correct syntax

Upvotes: 1

Related Questions