Remove suffix from filename in Swift

Confused picture Confused · Oct 6, 2016 · Viewed 12.5k times · Source

When trying to remove the suffix from a filename, I'm only left with the suffix, which is exactly not what I want.

What (how many things) am I doing wrong here:

let myTextureAtlas = SKTextureAtlas(named: "demoArt")

let filename = (myTextureAtlas.textureNames.first?.characters.split{$0 == "."}.map(String.init)[1].replacingOccurrences(of: "\'", with: ""))! as String

print(filename)

This prints png which is the most dull part of the whole thing.

Answer

Jovan Stankovic picture Jovan Stankovic · Oct 17, 2019

If by suffix you mean path extension, there is a method for this:

let filename = "demoArt.png"
let name = (filename as NSString).deletingPathExtension
// name - "demoArt"