Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iOS Swift2中读取本地HTML文件_Ios_Swift2 - Fatal编程技术网

在iOS Swift2中读取本地HTML文件

在iOS Swift2中读取本地HTML文件,ios,swift2,Ios,Swift2,我的应用程序中有一个站点文件夹。我组装了一个HTML字符串并将其加载到webview中。在这个过程中,我在加载两个文件时遇到了一个错误 let hpath: String = "site/header.html" let fpath: String = "site/footer.html" let head: String = String(contentsOfFile: hPath, encoding: NSUTF8StringEncoding, error: nil) let foot: S

我的应用程序中有一个站点文件夹。我组装了一个HTML字符串并将其加载到webview中。在这个过程中,我在加载两个文件时遇到了一个错误

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
错误:

无法使用类型为“(contentsOfFile:String,encoding:UInt,error:NilLiteralConvertible)”的参数列表调用类型“String”的初始值设定项


我的来源与我找到的示例相同。也许现在在Swift 2中有所不同。无论哪种方式,我都需要更改什么才能读取这两个文件的内容?

Swift 2中的错误处理已更改。如果需要在运行时处理错误并需要错误消息:

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}
如果您知道该文件将在运行时存在(例如,在应用程序包中):

如果文件不存在,上述操作将崩溃

第三种选择:

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

如果文件不存在,则不会崩溃,但会返回可选字符串,并且不会返回错误消息。

这就是我从添加到项目的目录中读取几个文件的方法。我将一个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;
}

查看
init(contentsOfFile path:String,encoding enc:UInt)抛出的文档。您还应该阅读Swift 2语言中关于do/try/catch的部分,以及它对于Objective-C中使用
NSError
的方法的含义。
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;
}