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

混江龙づ霸主 提交于 2019-12-01 14:20:17

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)
}

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.

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

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