将String转换为NSURL迅速返回nil

2020/12/15 22:32 · ios ·  · 0评论

我正在尝试将转换为StringNSURL而我的代码如下:

var url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=\(self.latitude),\(self.longitude)&destinations=\(self.stringForDistance)&language=en-US"
    println("This is String: \(url)")
    var remoteUrl : NSURL? = NSURL(string: url)
    println("This is URL: \(remoteUrl)")

控制台会输出如下内容:

This is String: https://maps.googleapis.com/maps/api/distancematrix/json?origins=-34.4232722,150.8865837&destinations=-34.4250728,150.89314939999997|-34.4356434,150.8858692|-34.4250728,150.89314939999997|-34.4356434,150.8858692|-34.4250728,150.89314939999997|-34.4356434,150.8858692|-34.423234,150.88658899999996|-34.423234,150.88658899999996|-34.428251,150.899673|-34.4257439,150.89870229999997|-34.423234,150.88658899999996|-34.4257439,150.89870229999997|-34.425376,150.89388299999996&language=en-US

This is URL: nil

remoteUrlnil,我不知道这里有什么问题。

之后,我尝试String像这样进行排序

var url : String = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=-34.4232722,150.8865837&destinations=-34.4250728,150.89314939999997&language=en-US"
    println("This is String: \(url)")
    var remoteUrl : NSURL? = NSURL(string: url)
    println("This is URL: \(remoteUrl)")

和控制台打印:

This is String: https://maps.googleapis.com/maps/api/distancematrix/json?origins=-34.4232722,150.8865837&destinations=-34.4250728,150.89314939999997&language=en-US
This is URL: Optional(https://maps.googleapis.com/maps/api/distancematrix/json?origins=-34.4232722,150.8865837&destinations=-34.4250728,150.89314939999997&language=en-US)

一切正常。

那么有人可以告诉我我的第一个案例有什么问题吗?

正如Martin R所建议的,我看到了这篇文章,并将那个Objective-C代码转换为swift,然后得到了以下代码:

var url : NSString = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=\(self.latitud‌​e),\(self.longitude)&destinations=\(self.stringForDistance)&language=en-US" 
var urlStr : NSString = url.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)! 
var searchURL : NSURL = NSURL(string: urlStr)! 
println(searchURL)

并且这正常工作。

对于Swift 3.0:

let url : NSString = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=\(self.latitud‌​e),\(self.longitude)&destinations=\(self.stringForDistance)&language=en-US"
let urlStr : NSString = url.addingPercentEscapes(using: String.Encoding.utf8.rawValue)! as NSString
let searchURL : NSURL = NSURL(string: urlStr as String)!
print(searchURL)

正如blwinters所说,在Swift 3.0中使用

URL(string: urlPath.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)

我认为尝试一下对我来说是完美的选择

  var url : String = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=-34.4232722,150.8865837&destinations=-34.4250728,150.89314939999997&language=en-US"
        println("This is String: \(url)")
        var urlStr : NSString = url.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
        var remoteUrl : NSURL? = NSURL(string: url)
        println("This is URL: \(remoteUrl!)")

如果NSURL为null并尝试通过Web视图加载http URL,则会出现以下错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

为了安全起见,我们应该使用:

 var urlStr = strLink!.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)

if var url = NSURL(string: urlStr!){
    println(self.strLink!)

    self.webView!.loadRequest(NSURLRequest(URL: url))

}

这项工作对我来说

let url : NSString = MyUrls.baseUrl + self.url_file_open as NSString
let urlStr : NSString = url.addingPercentEscapes(using: String.Encoding.utf8.rawValue)! as NSString
if let url = URL(string: urlStr as String) {
    let request = URLRequest(url: url)
    self.businessPlanView.loadRequest(request)
}

SWIFT 3.0

修复将不良字符串转换为NSURL的安全方法是使用“ guard let”解开urlPath字符串变量

guard let url = NSURL(string: urlPath.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
            else
            {
                print("Couldn't parse myURL = \(urlPath)")
                return
            }

在我上面的示例中,名为“ urlPath”的变量将是您已经在代码中其他位置声明的url字符串。

我遇到了这个答案,因为在将我的字符串制成NSURL的那一刻,我随机遇到了XCode中断的错误,并且报错。为什么没有随机性,即使我打印了网址也没有问题。一旦我添加了.addingPercentEncoding,它就可以正常工作而没有任何问题。

tl; dr对于阅读本文的任何人,请尝试我上面的代码,并将“ urlPath”换成您自己的本地字符串url。

本文地址:http://ios.askforanswer.com/jiangstringzhuanhuanweinsurlxunsufanhuinil.html
文章标签: ,   ,   ,  
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!

文件下载

老薛主机终身7折优惠码boke112

上一篇:
下一篇:

评论已关闭!