How to split filename from file extension in Swift?

前端 未结 21 1956
陌清茗
陌清茗 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 04:12

    Creates unique "file name" form url including two previous folders

    func createFileNameFromURL (colorUrl: URL) -> String {
    
        var arrayFolders = colorUrl.pathComponents
    
        // -3 because last element from url is "file name" and 2 previous are folders on server
        let indx = arrayFolders.count - 3
        var fileName = ""
    
        switch indx{
        case 0...:
            fileName = arrayFolders[indx] + arrayFolders[indx+1] + arrayFolders[indx+2]
        case -1:
            fileName = arrayFolders[indx+1] + arrayFolders[indx+2]
        case -2:
            fileName = arrayFolders[indx+2]
        default:
            break
        }
    
    
        return fileName
    }
    

提交回复
热议问题