带有空格的Swift本地html文件

时间:2015-12-03 07:03:30

标签: html swift uiwebview

我正在使用此代码在Web视图中加载HTML文件:

class DiseaseWVC: UIViewController {
    @IBOutlet weak var web2: UIWebView!

    var index : Int?

    override func viewDidLoad() {
        if let file = self.index{
            print("got: \(file)")
        }else{
            print("didn't get")
        }
        //"Ace-VetBolus", withExtension: "html", subdirectory: "HTMLFiles"
        let path: String? = NSBundle.mainBundle().pathForResource(getFileName(index!), ofType: "html", inDirectory: "HTMLFiles/disease")
        if let unwrappedPath = path {
            let requestURL = NSURL(string: unwrappedPath)
            let request = NSURLRequest(URL: requestURL!)

            web2.loadRequest(request)
        }
    }

    func getFileName(index: Int) -> String{
        var fileNames: [String] = [
            "Anthrax",
            "Avian Colibacillosis"
        ]

        return fileNames[index]
    }
}

但由于文件Black Leg有空格,因此无法加载。如何加载带空格的文件?

1 个答案:

答案 0 :(得分:0)

可能是路径错误,尝试在控制台中打印出路径,在Foundation.Frameworks中,它将具有NSPathUtilities, 使用此函数-NSSearchPathForDirectoriesInDomains和NSPathUtilities的ENUM来搜索文件的位置。