问题
I have to share the same data among 6 different apps so I thought a JSON file was the best way to save the data on each app.
I created a local file called "AppsInfo.json".
{
"1": {
"name": "app1",
"desc": "",
"amount": "19.99",
},
"2": {
"name": "app2",
"desc": "",
"amount": "14.99",
}
}
I can get the file path with this:
let filePath = Bundle.main.path(forResource: "AppsInfo", ofType: "json");
Now how do I load this file into something like SwiftyJSON?
Using Swift 4.2.
Thank you.
回答1:
First you read the Data
from the path, better return URL
of the resource
if let url = Bundle.main.url(forResource: "AppsInfo", withExtension: "json") {
do {
let data = try Data(contentsOf: url, options: .mappedIfSafe)
let json = JSON(data: data)
} catch {
// handle error
}
}
来源:https://stackoverflow.com/questions/55863489/swift-import-local-file-import-into-swiftyjson