Adrime Adrime - 1 year ago 66
iOS Question

Replace extension of a filename in swift

Given an string with format

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

In java it would be

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

Answer Source

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")!