118

I need url filepath be a URL (NSURL in old versions of Swift). 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
repeat {
    filePath =
    "\(documentsDirectory)/\(dateTimePrefix)-\(fileNamePostfix).mp4"
    fileNamePostfix += 1
} while (FileManager.default.fileExists(atPath: filePath))

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

I tried filePath as? URL() but it isn't correct.

6 Answers 6

251

you need to do:

let fileUrl = URL(string: filePath)

or

let fileUrl = URL(fileURLWithPath: filePath)

depending on your needs. See URL docs

Before Swift 3, URL was called NSURL.

Sign up to request clarification or add additional context in comments.

3 Comments

Swift 4: first one gives nil, second one works for paths like "/dir/file.ext". Thank you!
Why is this the case? Does anyone know?
The first option of this answer is wrong for a file path. It would only work if it's a full file:// URL string.
16

In swift 3 use:

let url = URL(string: "Whatever url you have(eg: https://google.com)")

1 Comment

thanks , it worked for me , earlier I was using URL.init(fileURLWithPath: <#T##String#>) which was producing wrong url. I think thats for some file path.
12

in swift 4 to convert to url use URL

let fileUrl = URL.init(fileURLWithPath: filePath)

or

let fileUrl = URL(fileURLWithPath: filePath)

Comments

10

In Swift3:
let fileUrl = Foundation.URL(string: filePath)

2 Comments

This will return nil if the path contains strings! Use let fileUrl = URL(fileURLWithPath: filePath) instead.
@natev not if the fileURL string contains the scheme "file://" as well if it was obtained using absoluteString instead of the path property.
4

To Convert file path in String to NSURL, observe the following code

var filePathUrl = NSURL.fileURLWithPath(path)

1 Comment

please, try to explain it as well in short.
0

Use String Extension for swift 5 for convert string into URL:

extension String {
    // Support Properties
    var absoluteURL: URL? {
        let _url = URL(string: self)
        return _url
    }
    
    var absoluteURLFromPath: URL? {
        let _url = URL(fileURLWithPath: self)
        return _url
    }
}

Usage

print(filePath.absoluteURL ?? filePath.absoluteURLFromPath ?? "Invalid") // It is convert path into URL

1 Comment

This is not for Swift 5. This would be valid even for Swift 3. And what's the benefit of this extension? It's no less code for the user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.