Path to bundle of iOS framework

后端 未结 4 1193
耶瑟儿~
耶瑟儿~ 2020-12-14 15:52

I am working on a framework for iOS, which comes with some datafiles. To load them into a Dictionary I do something like this:

public func loadP         


        
相关标签:
4条回答
  • 2020-12-14 16:27

    Simply specify the class name of the resource and below function will give you the Bundle object with which the class is associated with, so if class is associated with a framework it will give bundle of the framework.

    let bundle = Bundle(for: <YourClassName>.self)
    
    0 讨论(0)
  • Swift 5

    let bundle = Bundle(for: Self.self)
    let path = bundle.path(forResource: "filename", ofType: ".plist")
    
    0 讨论(0)
  • 2020-12-14 16:29

    Use Bundle(for:Type):

    let bundle = Bundle(for: type(of: self))
    let path = bundle.path(forResource: filename, ofType: type)
    

    Update:

    or search the bundle by identifier (the frameworks bundle ID):

    let bundle = Bundle(identifier: "com.myframework")
    
    0 讨论(0)
  • 2020-12-14 16:39

    Try below code to get the custom bundle:

    let bundlePath = Bundle.main.path(forResource: "CustomBunlde", ofType: "bundle")
    let resourceBundle = Bundle.init(path: bundlePath!)
    

    Update

    If in your framework, try this:

    [[NSBundle bundleForClass:[YourClass class]] URLForResource:@"YourResourceName" withExtension:@".suffixName"];
    
    0 讨论(0)
提交回复
热议问题