如何使用Swift 2和Xcode 7读取操场文本资源文件

时间:2015-06-20 18:27:51

标签: ios xcode swift-playground xcode7

Xcode 7 Playgrounds现在支持从嵌套 Resources目录中加载文件。

如果SKScene(fileNamed: "GameScene")GameScene.sks ResourcesNSImage(named:"GameScene.png") GameScene.png Resources Resources,我可以获得Bundle.main

但是如何从Playground // 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)") } 目录中读取常规文本文件呢?

6 个答案:

答案 0 :(得分:30)

我们可以使用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" } }

所以,如果你在游乐场有一个test.json,比如

enter image description here

您可以访问它并打印其内容:

<div class="box1">content</div>
<div class="box2">content</div>
<div class="box3">content</div>

将打印

data.result

答案 1 :(得分:10)

Jeremy Chone's回答,更新了Swift 3,Xcode 8:

['is very important']
['We value', 'since we need']

答案 2 :(得分:6)

您可以直接使用String和URL。 Swift 3中的示例:

let url = Bundle.main.url(forResource: "test", withExtension: "json")!
let text = String(contentsOf: url)

答案 3 :(得分:1)

为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
}

答案 4 :(得分:0)

另一个简短的方法(Swift 3):

window.addEventListener('resize', function(event){
            var w = window,
            d = document,
            e = d.documentElement,
            g = d.getElementsByTagName('body')[0],
            tableWidth = w.innerWidth || e.clientWidth || g.clientWidth,
            tableHeight = w.innerHeight|| e.clientHeight|| g.clientHeight;
             if(document.getElementById("tableHeader")!=null){
                if(tableWidth>1400){
                    document.getElementById("tableHeader").style.width = tableWidth-300+"px";
                    document.getElementById("tableBody").style.width = tableWidth-300+"px";
                }
            }
        });

答案 5 :(得分:0)

快速5

可以通过Playground中的捆绑包访问Resources文件夹中的文件。

import UIKit

有两种获取JSON数据的方法。

路径:

    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
    }