1
在一些流行的开源swift项目中。我注意到以下方法用于从主包加载文件。从mainBundle加载
@objc class TestClass: NSObject { }
let bundle = NSBundle(forClass: TestClass.self)
let path = bundle.pathForResource(filename, ofType: "json")
我们也可以使用这种方法。
let path = NSBundle.mainBundle().pathForResource(filename, ofType: "json")
为什么有人会选择第二种方法?
如果您的项目中有不同的软件包(比如库或框架),如果您使用第一种方法,则可以确保从外部软件包明确加载正确的内容。 – holex