Steven
Steven

Reputation: 782

how to get the rest of path from sub folder

I have a path like :

file:///private/var/mobile/Containers/Data/Application/80C93A3C-DFB5-4756-8DC8-7ED34469EA11/Documents/allFiles/81B5D904-CCEC-4B92-BD9B-4175BF6A99BC.jpeg

The thing that I am looking for is to have the path value after documents path :

file:///private/var/mobile/Containers/Data/Application/80C93A3C-DFB5-4756-8DC8-7ED34469EA11/Documents

like give me the path value after the documents folder: the response should be:

allFiles/81B5D904-CCEC-4B92-BD9B-4175BF6A99BC.jpeg

I didn't find API about this.

Upvotes: 0

Views: 70

Answers (1)

Joakim Danielson
Joakim Danielson

Reputation: 52088

If you have a String path then convert it into an URL,

let path = "file:///private/var/mobile/Containers/Data/Application/80C93A3C-DFB5-4756-8DC8-7ED34469EA11/Documents/allFiles/81B5D904-CCEC-4B92-BD9B-4175BF6A99BC.jpeg"
let url = URL(fileURLWithPath: path)

then split the url up into components

let components = url.pathComponents

and create a string from everything after the component "Documents"

if let index = components.firstIndex(of: "Documents") {
    print(components[components.index(index, offsetBy: 1)..<components.endIndex].joined(separator: "/"))
}

output

allFiles/81B5D904-CCEC-4B92-BD9B-4175BF6A99BC.jpeg

Upvotes: 1

Related Questions