“NSURLRequest?"没有名为“URL"的成员 - Swift [英] 'NSURLRequest?' does not have a member named 'URL' - Swift

查看:18
本文介绍了“NSURLRequest?"没有名为“URL"的成员 - Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Swift 编码真的很陌生,我正在尝试遵循本书中的代码:http://www.apress.com/9781484202098.学习 iOS 8 App Development 2nd Edition by James Bucanek

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

特别是,我正在完成第 3 章 - 构建一个 URL 缩短应用程序,但尽管已经完全复制了代码,但我在第 76 页的代码中遇到了错误:

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 {

哪个声明NSURLRequest?"没有名为URL"的成员.

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

我试过在谷歌上搜索答案,但不幸的是没有遇到任何问题.我能找到的任何回复似乎都表明我的代码应该正常工作(例如 如何获取我在 UIWebView 上点击的网址?).这似乎有最接近的答案 SWIFT:为什么我无法在 UIWebView 中加载当前 URL? 但该解决方案似乎对我不起作用.如果我添加一个 ?在请求之后,它至少会构建它,但是我返回了一个 nil 变量.

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.

我使用的是 Xcode v6.1.1.这是在 ViewController.swift 中出现错误的一段代码:

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!

更新:

根据下面 Ashley 的建议,我修改了我的代码,使其不再出现错误(请参阅上面的评论).但是,它现在不再运行.这似乎是因为 urlString 被创建为 http://api.x.co/Squeeze.svc/text/d558979bb9b84eddb76d8c8dd9740ce3?url=Optional("http://www.apple.com/").因此,问题在于包含的 Optional() 使其成为无效的 URL.有没有人有关于如何删除它的建议?

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?

推荐答案

requestUIWebView 上的可选属性:

request is an optional property on UIWebView:

var request: NSURLRequest? { get }

还有 stringByAddingPercentEscapesUsingEncoding 返回一个可选的:

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
        }

    }
}

参见 Apple 关于可选链的文档 详情

参见 NSURL 类的 Apple 文档

See Apple's docs for NSURL class

这篇关于“NSURLRequest?"没有名为“URL"的成员 - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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