Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error : Value of type 'String' has no member 'URLByAppendingPathComponent'

Tags:

ios

swift

My Error is: Value of type 'String' has no member 'URLByAppendingPathComponent'

I got error in this line :

 let savePath = documentDirectory.URLByAppendingPathComponent("mergeVideo-\(date).mov")

My full code:

  // 4 - Get path
  let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] 
  var dateFormatter = NSDateFormatter()
  dateFormatter.dateStyle = .LongStyle
  dateFormatter.timeStyle = .ShortStyle
  let date = dateFormatter.stringFromDate(NSDate())
  let savePath = documentDirectory.URLByAppendingPathComponent("mergeVideo-\(date).mov")

    let url = NSURL(fileURLWithPath: savePath)

I followed this tutorial : Here

like image 664
user5513630 Avatar asked Feb 09 '23 13:02

user5513630


2 Answers

It's

let savePath = (documentDirectory as NSString).stringByAppendingPathComponent("mergeVideo-\(date).mov")

since documentDirectory is a String rather than an NSURL

Edit

I recommend to use this API:

let documentDirectory = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false)
var dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = .LongStyle
dateFormatter.timeStyle = .ShortStyle
let date = dateFormatter.stringFromDate(NSDate())
let saveURL = documentDirectory.URLByAppendingPathComponent("mergeVideo-\(date).mov") // now it's NSURL

Swift 3+

let documentDirectory = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
var dateFormatter = DateFormatter()
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .short
let date = dateFormatter.string(from: Date())
let saveURL = documentDirectory.appendingPathComponent("mergeVideo-\(date).mov")
like image 62
vadian Avatar answered Mar 16 '23 00:03

vadian


As the error states, there is no URLByAppendingPathComponent method available for String class, that function belongs to NSURL.

You need to use:

let savePath = (documentDirectory as NSString).stringByAppendingPathComponent("mergeVideo-\(date).mov")

Or you can do it like:

let url      = NSURL(fileURLWithPath: documentDirectory)
let savePath = url.URLByAppendingPathComponent("mergeVideo-\(date).mov")
like image 44
Midhun MP Avatar answered Mar 15 '23 23:03

Midhun MP