"NSURL"不能隐式转换为"URL";您是不是要使用“作为"明确转换? [英] "NSURL" is not implicitly convertible to "URL"; did you mean to use "as" to explicitly convert?

查看:325
本文介绍了"NSURL"不能隐式转换为"URL";您是不是要使用“作为"明确转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有最新的xcode beta,只是试图在应用程序中加载网页.

I have latest xcode beta, just trying to load a webpage inside an app.

import UIKit

class ViewController: UIViewController {

    @IBOutlet var webView: UIWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let url = NSURL (string: "https://www.google.com");
        let requestObj = NSURLRequest(URL: url!);
        webView.loadRequest(requestObj);
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

在此行上:let requestObj = NSURLRequest(URL: url!); 我收到以下错误:

On this line: let requestObj = NSURLRequest(URL: url!); I get following error:

"NSURL"不能隐式转换为"URL";您是要使用"as"来进行显式转换吗?

"NSURL" is not implicitly convertible to "URL"; did you mean to use "as" to explicitly convert?

我删除了感叹号,但出现此错误:

I removed the exclamation mark and I get this error:

无法转换类型为"NSURL"的值?预期的参数类型"URL"

Cannot convert value of type "NSURL?" to expected argument type "URL"

在过去的四个小时中,我一直在尝试从Internet遍历此代码.我在想最新的Swift不允许这样做了,因为什么都没有用.我设置为10.0部署目标和Xcode 8.0兼容.但是我也尝试了这两个设置的早期版本,并且在这两个版本的所有版本中都遇到了相同的错误.

I've been trying variations on this code from all over the internet for the past four hours. I'm thinking latest swift just doesn't allow this anymore or something, because nothing works. I'm set to a 10.0 Deployment Target and a Xcode 8.0 compatible. But I've also tried earlier versions for both of those settings and I get the same error across all versions of both.

是的,我在情节提要上的webView已正确连接到ViewController.swift.

Yes, my webView on the storyboard is properly connected to the ViewController.swift.

推荐答案

在Swift 3中,您需要使用URL而不是NSURL,因此创建这样的url对象.

In swift 3 you need to use URL instead of NSURL, so create url object like this.

if let url = URL(string: "https://www.google.com") {
    webView.load(URLRequest(url: url))
}

这篇关于"NSURL"不能隐式转换为"URL";您是不是要使用“作为"明确转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆