How to split filename from file extension in Swift?

前端 未结 21 1906
陌清茗
陌清茗 2020-12-01 03:24

Given the name of a file in the bundle, I want load the file into my Swift app. So I need to use this method:

let soundURL = NSBundle.mainBundle().URLForReso         


        
21条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 03:51

    A better way (or at least an alternative in Swift 2.0) is to use the String pathComponents property. This splits the pathname into an array of strings. e.g

    if let pathComponents = filePath.pathComponents {
        if let last = pathComponents.last {
            print(" The last component is \(last)") // This would be the extension
            // Getting the last but one component is a bit harder
            // Note the edge case of a string with no delimiters!
        }
    }
    // Otherwise you're out of luck, this wasn't a path name!
    

提交回复
热议问题