Ios 向JSON添加本地化后加载JSON失败Swift 3

Ios 向JSON添加本地化后加载JSON失败Swift 3,ios,json,swift,xcode,swift3,Ios,Json,Swift,Xcode,Swift3,嘿,我的应用程序中有一个用于预设数据的JSON。通常我加载的数据如下所示: if let path = Bundle.main.path(forResource: "document", ofType: "json") { let jsonData = try NSData(contentsOfFile: path, options: NSData.ReadingOptions.mappedIfSafe) ... } 但在我将document.j

嘿,我的应用程序中有一个用于预设数据的JSON。通常我加载的数据如下所示:

        if let path = Bundle.main.path(forResource: "document", ofType: "json")

    {

        let jsonData = try NSData(contentsOfFile: path, options: NSData.ReadingOptions.mappedIfSafe)
  ... }
但在我将document.json文件本地化后,如下所示:

没有任何数据无法加载。
有人知道如何获取本地化数据吗

由于本地化,本地化文件位于
lproj
文件夹中,而不是位于mein
Resources
文件夹中

解决方案是使用支持的语言创建一个数组,并从当前语言环境中获取当前语言。如果当前语言与支持的语言之一不匹配,请返回默认语言

let supportedLocalizations = ["en", "de"]

let currentLanguage : String
if let locale = Locale.current.languageCode, supportedLocalizations.contains(locale) {
    currentLanguage = locale
} else {
    currentLanguage = Bundle.main.object(forInfoDictionaryKey: "CFBundleDevelopmentRegion") as! String
}
let url = Bundle.main.url(forResource: "document", withExtension: "json", subdirectory: nil, localization: currentLanguage)!
let jsonData = try! Data(contentsOf: url)
如果强制展开的零件发生碰撞,则表明存在设计错误。所有文件都应该在运行时存在