How to get path to a subfolder in main bundle?

后端 未结 3 444
Happy的楠姐
Happy的楠姐 2020-12-19 00:53

I have a project which I am migrating from Obj-C to Swift 3.0 (and I am quite a noob in Swift).

How do I translate this line?

NSString *folder = [[[N         


        
相关标签:
3条回答
  • 2020-12-19 01:12

    In Swift-3 make URL, and 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 on information on stringByAppendingPathComponent method in Swift.

    0 讨论(0)
  • 2020-12-19 01:27

    You can do something like this:

    let folderURL = resourceURL(to: "myfolder")
    
    func resourceURL(to path: String) -> URL? {
        return URL(string: path, relativeTo: Bundle.main.resourceURL)
    }
    
    0 讨论(0)
  • 2020-12-19 01:29

    You can use this method to get listing of the bundle subdirectory and get resources only of the specific type:

    Bundle.main.paths(forResourcesOfType: type, inDirectory: folder)
    
    0 讨论(0)
提交回复
热议问题