ios - Change URL last component name and add extension to the renamed one -
i have following url
file:///users/ivancantarino/library/developer/coresimulator/devices/cce2fca5-05f0-4bb7-9a25-cbc168398a62/data/containers/data/application/e48179f9-84fb-4cb3-b993-1e33fcfb5083/library/caches/cloudkit/34af81edf8344be1cf473ef394e06ccc8e1bc8cf/assets/ae86e028-e4e0-45d5-b5fe-bae975d07f2a.0166f7df06d2562d8d53e70a3779a46de3769584c3
i'm trying rename last part component of url, still having url not filename.
i've managed access last part following code:
func extractandbreakfilenameincomponents(fileurl: nsurl) { let fileurlparts = fileurl.path!.components(separatedby: "/") let filename = fileurlparts.last print("filename:", filename) // prints: ae86e028-e4e0-45d5-b5fe-bae975d07f2a.0166f7df06d2562d8d53e70a3779a46de3769584c3 }
i want change filename
myfile.pdf
keeping url
it simple url rename, not creating new one, changing existing one
what's best approach on this?
thank you
you can way
var url = url.init(string: "http://www.example.com/test123") url?.deletelastpathcomponent() url?.appendpathcomponent("file.pdf") print(url)
output :http://www.example.com/file.pdf
hope helps you
edit
you need rename file use following code
1) create copy , update last component
var urlnew = url.init(string: "http://www.example.com/test123") urlnew?.deletelastpathcomponent() urlnew?.appendpathcomponent("file.pdf") { try filemanager.default.moveitem(atpath: oldurl!, topath: urlnew) } catch { }
Comments
Post a Comment