How to get the filename from the filepath in swift

后端 未结 10 1506
一生所求
一生所求 2020-12-04 20:47

How to get the filename from the given file path string?

For example if I have a filepath string as

file:///Users/DeveloperTeam/Library/Developer/Co         


        
10条回答
  •  一生所求
    2020-12-04 21:03

    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
    

    }

提交回复
热议问题