Search code examples
iosnsbundle

Get path to assets file in iOS: NSBundle is missing


I am trying to get the path to a file I have in the Assets.xcassets directory for my iOS app, but I am not able to. It seems like I should use NSBundle.mainBundle() somehow, but Xcode cannot find NSBundle even though I import Foundation or UIKit. If I do auto completion on "NSBundle" I get the following options:

NSBundleErrorMaximum
NSBundleErrorMinimum
NSBundleResourceRequest
NSBundleExecutableArchitecturePPC
NSBundleExecutableArchitectureI386
NSBundleExecutableArchitecturePPC64
NSBundleExecutableArchitectureX86_64
NSBundleOnDemandResourceInvalidTagError

I have no clue what these are for. So my questions are:

  1. How can I import NSBundle into my project?
  2. How can I get the path to the following file: Assets.xcassets/Database.dataset/database.txt?

Note that I am able to get the contents of the file doing the following, but I want the path to the file.

fileprivate static func readDatabaseFileFromAssets() -> NSDictionary
{
    let databaseAsset = NSDataAsset(name: "Database", bundle: Bundle.main)
    let json = try? JSONSerialization.jsonObject(with: databaseAsset!.data, options: JSONSerialization.ReadingOptions.allowFragments)
    return json as! NSDictionary
}

Solution

  • Many things have changed names in Swift3!

    let url = Bundle.main.url(forResource: "Database", withExtension: "txt")
    // url.path