我正在使用以下代码在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具有空间,因此不会被加载。如何用空格加载文件?

最佳答案

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

10-07 19:56
查看更多