简体   繁体   English

在iOS Swift2中读取本地HTML文件

[英]Read local HTML file in iOS Swift2

I have a site folder in my app. 我的应用中有一个站点文件夹。 I assemble an HTML string and load it in a webview. 我组装了一个HTML字符串并将其加载到webview中。 In the process, I'm running into an error trying to load two files. 在这个过程中,我遇到了一个错误,试图加载两个文件。

let hpath: String = "site/header.html"
let fpath: String = "site/footer.html"
let head: String = String(contentsOfFile: hPath, encoding: NSUTF8StringEncoding, error: nil)
let foot: String = String(contentsOfFile: fPath, encoding: NSUTF8StringEncoding, error: nil)
return head + foot

Error: 错误:

Cannot invoke initializer for type 'String' with an argument list of type '(contentsOfFile: String, encoding: UInt, error: NilLiteralConvertible)' 无法使用类型为'(contentsOfFile:String,encoding:UInt,error:NilLiteralConvertible)的参数列表调用类型'String'的初始值设定项

My source is the same as examples I found. 我的来源与我发现的例子相同。 Maybe it's different now in Swift 2. Either way, what needs to change so I can read the content of these two files? 也许它现在在Swift 2中有所不同。无论哪种方式,需要改变什么,以便我可以阅读这两个文件的内容?

The error handling in Swift 2 has changed. Swift 2中的错误处理已更改。 If you need to handle the error at runtime and need the error message: 如果需要在运行时处理错误并需要错误消息:

var head: String
do {
    head = try String(contentsOfFile: hPath, encoding: NSUTF8StringEncoding)
}
catch let error as NSError { fatalError(error.localizedDescription) // or do something else with the error}

If you know the file will exist at runtime (eg in the application bundle): 如果您知道该文件将在运行时存在(例如在应用程序包中):

let foot: String = try! String(contentsOfFile: fPath, encoding: NSUTF8StringEncoding)

The above will crash if the file does not exist. 如果文件不存在,上述内容将崩溃。

Third option: 第三种选择:

let foot: String? = try? String(contentsOfFile: fPath, encoding: NSUTF8StringEncoding)

This won't crash if the file does not exist, but returns an optional string and no error message. 如果文件不存在,则不会崩溃,但会返回可选字符串,并且不会显示错误消息。

This is how I was able to read a couple files from a directory I added to my project. 这就是我能够从我添加到项目中的目录中读取几个文件的方法。 I pass a body of HTML to a method, then I wrap it with a header and footer stored in the app's file system. 我将一个HTML主体传递给一个方法,然后我用一个存储在应用程序文件系统中的页眉和页脚来包装它。

func assembleHTML(var html: String) -> String {
    let fileMgr = NSFileManager.defaultManager()
    let hPath = NSBundle.mainBundle().pathForResource("site/header", ofType: "html")!
    let fPath = NSBundle.mainBundle().pathForResource("site/footer", ofType: "html")!
    var hContent: String?
    var fContent: String?
    if fileMgr.fileExistsAtPath(hPath) && fileMgr.fileExistsAtPath(fPath) {
        do {
            hContent = try String(contentsOfFile: hPath, encoding: NSUTF8StringEncoding)
            fContent = try String(contentsOfFile: fPath, encoding: NSUTF8StringEncoding)
        } catch let error as NSError {
            print("error:\(error)")
        }
    } else {
        print("not found")
    }
    html = hContent! + html + fContent!
    return html;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM