Howard Howard - 4 months ago 8x
iOS Question

Breaking change to get filename without extension in Swift2

In Swift1, we can get file short name without extension by following code: = pathFilename.lastPathComponent.stringByDeletingPathExtension

While I updated to Swift 2, this API is not available anymore. With the warning message, I have to use NSURL. So the new code will be:

var filename = NSURL(fileURLWithPath: str).lastPathComponent
filename = NSURL(fileURLWithPath: filename!).URLByDeletingPathExtension?.relativePath

It is too complex API breaking change. Is there a better way that could make it simpler?


Why not: = NSURL(fileURLWithPath: str).URLByDeletingPathExtension?.lastPathComponent

I'm not fluent in Swift so there may be some missing ! or ? needed in there.