'NSURLRequest?' does not have a member nam

2019-08-03 09:02发布

Hi I am really new to coding in Swift, and am trying to follow the codes in this book: http://www.apress.com/9781484202098. Learn iOS 8 App Development 2nd Edition by James Bucanek

In particular, I am working through Chapter 3 - building a URL shortening app, but despite having copied the code exactly, I am getting an error on the code in Page 76:

if let toShorten = webView.request.URL.absoluteString {

which states 'NSURLRequest?' does not have a member named 'URL'.

I have tried googling an answer, but unfortunately have not come across anything. Any response I can find seems to suggest that my code ought to be working (e.g. How to get url which I hit on UIWebView?). This seems to have the closest answer SWIFT: Why I can't get the current URL loaded in UIWebView? but the solution does not appear to work for me. If I add a ? after the request, it will then at least build it, but I then have a nil variable returned.

I am using Xcode v6.1.1. Here is the piece of code that is coming up with the error in ViewController.swift:

    let GoDaddyAccountKey = "0123456789abcdef0123456789abcdef" //this is replaced by my actual account key in my own code
    var shortenURLConnection: NSURLConnection?
    var shortURLData: NSMutableData?

    @IBAction func shortenURL( AnyObject ) {

        if let toShorten = webView.request?.URL.absoluteString { // ? now added

        let encodedURL = toShorten.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
        let urlString = "http://api.x.co/Squeeze.svc/text/\(GoDaddyAccountKey)?url=\(encodedURL)"
        shortURLData = NSMutableData()
        if let firstrequest = NSURL(string: urlString) //added if here and removed !
        let request = NSURLRequest(URL:firstrequest)
        shortenURLConnection = NSURLConnection(request:request, delegate:self)
        shortenButton.enabled = false
        }
        }
    }

If you have any suggestions on how I can fix this, I would really appreciate it!

Update:

Following suggestions from Ashley below, I have amended my code so that it is no longer bringing up the error (see comments above). However, it is now no longer running. This appears to be because the urlString is being created as http://api.x.co/Squeeze.svc/text/d558979bb9b84eddb76d8c8dd9740ce3?url=Optional("http://www.apple.com/"). The problem is therefore the Optional() that is included and thus makes it an invalid URL. Does anyone have a suggestion on how to remove this please?

1条回答
萌系小妹纸
2楼-- · 2019-08-03 09:18

request is an optional property on UIWebView:

var request: NSURLRequest? { get }

also stringByAddingPercentEscapesUsingEncoding returns an optional:

func stringByAddingPercentEscapesUsingEncoding(_ encoding: UInt) -> String?

What you need is to make user of optional binding in a few places:

if let toShorten = webView.request?.URL.absoluteString {

    if let encodedURL = toShorten.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) {

        let urlString = "http://api.x.co/Squeeze.svc/text/\(GoDaddyAccountKey)?url=\(encodedURL)"
        shortURLData = NSMutableData()

        if let firstrequest = NSURL(string: urlString) {  // If a method can return a nil, don't force unwrap it
            let request = NSURLRequest(URL:first request)
            shortenURLConnection = NSURLConnection(request:request, delegate:self)
            shortenButton.enabled = false
        }

    }
}

See Apple's docs on optional chaining for details

See Apple's docs for NSURL class

查看更多
登录 后发表回答