I've created a playground book on swift playgrounds, but I don't know how to add a JSON file to the directory. Can you add JSON files to swift playgrounds (ipad)? If so, how?
Asked
Active
Viewed 2,946 times
1
-
You can add files to the resources folder of your playground. – koen May 12 '20 at 14:43
1 Answers
4
Click on the '+' icon on the top right corner of the swift Playgrounds
and choose the json file by clicking Insert from...
option. Then use the below code to read the data from json
file.
import Foundation
do {
guard let fileUrl = Bundle.main.url(forResource: "filename", withExtension: "json") else { fatalError() }
let text = try String(contentsOf: fileUrl, encoding: String.Encoding.utf8)
print(text)
} catch {
print(error)
}

Vinayaka S Yattinahalli
- 716
- 6
- 17