Howard Howard - 5 months ago 18
iOS Question

Breaking change to get filename without extension in Swift2

In Swift1, we can get file short name without extension by following code:

self.name = 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?

Answer

Why not:

self.name = NSURL(fileURLWithPath: str).URLByDeletingPathExtension?.lastPathComponent

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