Tuesday, March 3, 2015

Converting file url to and from strings with swift

There times where you need to display a file url as a string. This might be to display a list of files into a Table View or even a field. 

Below is a custom function I made to do this. It takes one parameter a NSURL and returns a string as a absolute file path

func myFileString(theFile: NSURL) -> String {

return theFile.absoluteString

}


And if you need to convert the string as a absolute file path back to a NSURL this custom function does that. It takes one parameter a string as a absolute file path.

func myFileStringURL(tFileString: String) -> NSURL {

return NSURL(fileURLWithPath: tFileString!)

}


As you can see not very hard, but useful. Making custom functions for later reuse of code for future project can be helpful. These work for Swift and Mac OS X with Swift v 1.1

Thanks, and happy coding ;)


1 comment:

  1. Hi there! As a developer, you may appreciate an online localization tool like https://poeditor.com/ which can let you easily manage your translation projects.

    ReplyDelete