Digvijaysinh Gida
Digvijaysinh Gida

Reputation: 411

How to change file extension using swift?

I'm using swift code but is not changed file extension.

This my swift code

let url = NSURL(fileURLWithPath: outputURL.path).deletingPathExtension?.appendingPathExtension("mp3")

Upvotes: 9

Views: 5684

Answers (1)

Sweeper
Sweeper

Reputation: 271965

You are just creating a URL and then changing that URL. You are not actually operating on any file.

To rename a file, call FileManager.default.moveItem(at:to:). Pass the file URL as the first argument, and the file URL with the new name as the second argument.

let newURL = outputURL.deletingPathExtension().appendingPathExtension("mp3")
try FileManager.default.moveItem(at: outputURL, to: newURL)

Upvotes: 17

Related Questions