How to get the filename from the filepath in swift

后端 未结 10 1505
一生所求
一生所求 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:06

    Try this

    let filename: String = "your file name"
    let pathExtention = filename.pathExtension
    let pathPrefix = filename.stringByDeletingPathExtension
    

    Updated :

    extension String {
        var fileURL: URL {
            return URL(fileURLWithPath: self)
        }
        var pathExtension: String {
            return fileURL.pathExtension
        }
        var lastPathComponent: String {
            return fileURL.lastPathComponent
        }
    }
    

    Hope it helps.

提交回复
热议问题