user3745888 user3745888 - 2 years ago 444
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 =
} 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


Answer Source

you need to do:

let fileUrl = NSURL(string: filePath)


let fileUrl = NSURL(fileURLWithPath: filePath)

depending on your needs. See NSURL docs

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download