Xcode 7 Playgrounds now supports loading files from the nested Resources
directory.
I can get SKScene(fileNamed: "GameScene")
when I have a GameScene.sks
in my Resources
or NSImage(named:"GameScene.png")
if I have a GameScene.png
in your Resources
.
But how can I read a regular text file from the Playground Resources
directory as well?
We can use the Bundle.main
So, if you have a test.json in your playground like
You can access it and print its content like that:
// get the file path for the file "test.json" in the playground bundle
let filePath = Bundle.main.path(forResource:"test", ofType: "json")
// get the contentData
let contentData = FileManager.default.contents(atPath: filePath!)
// get the string
let content = String(data:contentData!, encoding:String.Encoding.utf8)
// print
print("filepath: \(filePath!)")
if let c = content {
print("content: \n\(c)")
}
Will print
filepath: /var/folders/dm/zg6yp6yj7f58khhtmt8ttfq00000gn/T/com.apple.dt.Xcode.pg/applications/Json-7800-6.app/Contents/Resources/test.json
content:
{
"name":"jc",
"company": {
"name": "Netscape",
"city": "Mountain View"
}
}
Jeremy Chone's answer, updated for Swift 3, Xcode 8:
// get the file path for the file "test.json" in the playground bundle
let filePath = Bundle.main.path(forResource: "test", ofType: "json")
// get the contentData
let contentData = FileManager.default.contents(atPath: filePath!)
// get the string
let content = String(data: contentData!, encoding: .utf8)
// print
print("filepath: \(filePath!)")
if let c = content {
print("content: \n\(c)")
}
You can use String directly with a URL. Example in Swift 3:
let url = Bundle.main.url(forResource: "test", withExtension: "json")!
let text = String(contentsOf: url)
Another short way (Swift 3):
let filePath = Bundle.main.path(forResource: "test", ofType: "json")
let content: String = String(contentsOfFile: filePath!, encoding: .utf8)
Added try for swift3.1:
let url = Bundle.main.url(forResource: "test", withExtension: "json")!
// let text = String(contentsOf: url)
do {
let text = try String(contentsOf: url)
print("text: \n\(text)")
}
catch _ {
// Error handling
}
// --------------------------------------------------------------------
let filePath2 = Bundle.main.path(forResource: "test", ofType: "json")
do {
let content2: String = try String(contentsOfFile: filePath2!, encoding: .utf8)
print("content2: \n\(content2)")
}
catch _ {
// Error handling
}
Swift 5
It is possible to get to files in your Resources
folder by the bundle in a Playground.
import UIKit
Here are two ways to get the JSON data.
Path:
guard let path = Bundle.main.path(forResource:"test", ofType: "json"),
let data = FileManager.default.contents(atPath: path) else {
fatalError("Can not get json data")
}
URL:
guard let url = Bundle.main.url(forResource:"test", withExtension: "json") else {
fatalError("Can not get json file")
}
if let data = try? Data(contentsOf: url) {
// do something with data
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.