Adrime Adrime - 3 months ago 6
iOS Question

Replace extension of a filename in swift

Given an string with format

filename.fileextension
I want to replace the fileextension with
newextension
. How can I do it?

In java it would be

foo.substring(0, foo.lastIndexOf(".")) + ".newextension"

Answer

stringByAppendingPathExtension no longer works in Swift 2.0 (if you are using String). But it does still work for NSString. So you can do:

var storePath = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory , .UserDomainMask, true)[0]) as NSString
storePath = storePath.stringByAppendingPathExtension("model.sqlite")!