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
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")
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With