Howard Howard -5 years ago 230
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?

Answer Source

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download