Why does using Nsurl request keeps telling me to rename to “init(url)”

落花浮王杯 提交于 2019-12-01 12:54:52

问题


Also trying to add the ability to detect a video and download it to the application in short a web-based app used for download any kind of video and has the ability to store it within the app

import UIKit

class ViewController: UIViewController {

    @IBOutlet var Webview: UIWebView!
    @IBOutlet var SearchBar: UISearchBar!

    override func viewDidLoad() {
       let url = NSURL(string: "https://www.google.com")
        let request = NSURLRequest(URL: url! as URL) Webview.loadRequest(request)
        SearchBar.text = "http://"
    }

    func searchBarSearchButtonClicked(searchbar: UISearchBar) {
        searchbar.resignFirstResponder()
        let text = SearchBar.text
        let url = NSURL(string: text!)
        let urlRequest:URLRequest = URLRequest(url: url! as URL)

        //  let request = NSURLRequest(URL: url! as URL)

        Webview.loadRequest(urlRequest)
    }

}

回答1:


You need to use like this in swift

if let url = URL(string: "https://www.google.com"){
    let requestObj = URLRequest(url: url)
    Webview.loadRequest(requestObj)
}



回答2:


URL :

URL is a swift struct, so is passed by value.

NSURL :

NSURL is an Objective-C class. Is inherits from NSObject

  • In general, prefer the new struct versions of things unless you need to subclass for some reason.
  • An object representing the location of a resource that bridges to URL; use NSURL when you need reference semantics or other Foundation-specific behavior.

Both URL and NSURL is accepted in swift. but you have used swift then most refer URL MORE.




回答3:


Use URL and URLRequest instead NSURL and NSURLRequest in Swift.

let url = URL.init(string: "https://www.google.com")
let request = URLRequest.init(url: url!)
Webview.loadRequest(request)

For Reference you can refer : Reference



来源:https://stackoverflow.com/questions/52252271/why-does-using-nsurl-request-keeps-telling-me-to-rename-to-initurl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!