0
我已將這些.plist和.json文件保留在我的項目中。我可以從Xcode讀取.plist文件。儘管無法讀取Swift 3.0上的.json文件。我已經使用下面的代碼片段來獲取這個。但不能這樣做。是否有可能從.json文件獲取數據? 請分享您的建議。Swift 3.0 - 從XCode包中讀取.JSON文件
可售plist文件
let plistFile = Bundle.main.path(forResource: "content", ofType: "plist")
let dictPlist = NSDictionary(contentsOfFile: plistFile!)
print(dictPlist ?? "")
無法提取JSON文件
if let filepath = Bundle.main.path(forResource: "fruits", ofType: "json") {
do {
let contents = try String(contentsOfFile: filepath)
print(contents)
} catch {
print("Fetched")
}
} else {
print("not found")
}
檢查這個https://stackoverflow.com/questions/45882205/load-json-from-file-with-swift-3/45882685#45882685 –
[Load from json from swift 3]可能的副本(https://stackoverflow.com/questions/45882205/load- json-from-file-with-swift-3) –
加載'Data'不是'String'(並使用'Bundle'的URL相關API)。無論如何你需要'數據'來進行反序列化。 – vadian