我正在使用此代码在 Web View 中加载 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 来搜索您的文件在哪里。
关于html - 带空格的 Swift 本地 html 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34059732/