user3745888 user3745888 - 7 months ago 137
Swift Question

How to convert this var string to NSURL in swift

I need url filepath be a NSURL. I have this:

let paths = NSSearchPathForDirectoriesInDomains(
.DocumentDirectory, .UserDomainMask, true)

// NSString *documentsDirectory = [paths objectAtIndex:0];
let documentsDirectory = paths[0] as String

var filePath:String? = nil
var fileNamePostfix = 0
do {
filePath =
"\(documentsDirectory)/\(dateTimePrefix)-\(fileNamePostfix++).mp4"
} while (NSFileManager.defaultManager().fileExistsAtPath(filePath))


I need to convert this to NSURL for use in self.fileOutput.startRecordingToOutputFileURL(<#outputFileURL: NSURL?#>, recordingDelegate: <#AVCaptureFileOutputRecordingDelegate?#>) method.

I tried
filePath as? NSURL()
but isn't correct

Thanks!

Answer

you need to do:

let fileUrl = NSURL(string: filePath)

or

let fileUrl = NSURL(fileURLWithPath: filePath)

depending on your needs. See NSURL docs