How to get the path to the subfolder in the main set?

I have a project from which I am transitioning from Obj-C to Swift 3.0 (and I'm fairly new to Swift).

How to translate this line?

NSString *folder = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"myfolder"];

I managed to get the path to the resources:

let resoursePath = Bundle.main.resoursePath;

But how do I get the path to a subfolder called "myfolder"? I need to get the path to a subfolder, not the file inside it.

+4
source share
2 answers

In Swift-3, make URLand call appendingPathComponent:

let resourcePath = Bundle.main.resourcePath
let subdir = URL(fileURLWithPath:resourcePath!).appendingPathComponent("sub").path

or simply

let subdir = Bundle.main.resourceURL!.appendingPathComponent("sub").path

(thanks Martin R !)

See this Q&A for method information stringByAppendingPathComponentin Swift.

+9
source

bundle :

Bundle.main.paths(forResourcesOfType: type, inDirectory: folder)
0

Source: https://habr.com/ru/post/1661280/


All Articles