【问题标题】:Swift 3 NSURL html Error does not compileSwift 3 NSURL html 错误无法编译
【发布时间】:2016-08-30 01:32:29
【问题描述】:

我得到错误“NSBundle has been changed to “Bundle”,但即使改变了,问题也没有解决。猜猜是别的什么?

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var WebVIew: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        WebVIew.loadRequest(NSURLRequest(url: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("HtmlCOde"), ofType: "html")!)!)
    }

}

【问题讨论】:

  • 请注意,资源名称后有一个额外的括号。你应该使用 Bundle.main.url(forResource:)
  • @LeoDabus WebVIew.loadRequest(NSURLRequest(url: NSURL(fileURLWithPath: Bundle.main.urlForResource("HtmlCode", withExtension: "html")!)!))
  • @LeoDabus 我将下载 beta 6 。当前代码: WebVIew.loadRequest(NSURLRequest(url: Bundle.main().url(forResource:"HtmlCode", withExtension: "html"))))
  • WebVIew.loadRequest(URLRequest(url: Bundle.main.url(forResource: "HtmlCode", withExtension: "html")!))
  • 使用URL,而不是NSURL。使用URLRequest,而不是NSURLRequest

标签: html swift ios9 swift3


【解决方案1】:

您应该使用URL 代替NSURLURLRequest 代替NSURLRequestBundle 而不是NSBundle

@IBOutlet weak var webView: UIWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    webView.loadRequest(URLRequest(url: URL(fileURLWithPath: Bundle.main.path(forResource: "HtmlCode", ofType: "html")!)))
}

或者,最好不要使用path

override func viewDidLoad() {
    super.viewDidLoad()

    webView.loadRequest(URLRequest(url: Bundle.main.url(forResource: "HtmlCode", withExtension: "html")!))
}

我还将WebVIew 的名称更改为webView,因为属性/变量通常应以小写字母开头(但请确保删除旧插座并在新属性中添加新插座,如果您这样做)。

【讨论】:

    猜你喜欢
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    • 2017-01-15
    • 2017-08-09
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多