Convert String to NSURL is return nil in swift

前端 未结 6 527
你的背包
你的背包 2020-12-01 09:15

I am trying to convert a String to NSURL and my code for that is Below:

var url = \"https://maps.googleapis.com/maps/api/distancema         


        
相关标签:
6条回答
  • 2020-12-01 09:22

    You can get following error if NSURL is null and trying load http URL over web view:

    fatal error: unexpectedly found nil while unwrapping an Optional value
    

    To be safe we should use:

     var urlStr = strLink!.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
    
    if var url = NSURL(string: urlStr!){
        println(self.strLink!)
    
        self.webView!.loadRequest(NSURLRequest(URL: url))
    
    }
    
    0 讨论(0)
  • 2020-12-01 09:28

    as blwinters said, in Swift 3.0 use

    URL(string: urlPath.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
    
    0 讨论(0)
  • 2020-12-01 09:32

    this work for me

    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)
    }
    
    0 讨论(0)
  • 2020-12-01 09:34

    I think try this it's perfectly work for me

      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!)")
    
    0 讨论(0)
  • 2020-12-01 09:42

    As suggested by the Martin R, I see THIS post and I converted that objective-c code to swift and I got this code:

    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)
    

    and this is working correctly.

    For 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)
    
    0 讨论(0)
  • 2020-12-01 09:49

    SWIFT 3.0

    A safe way to fix a bad string being converted to NSURL is by unwrapping the urlPath string variable using "guard let"

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

    The variable called "urlPath" in my above example would be the url string you have already declared somewhere else in your code.

    I came across this answer because randomly I was getting the nil error with XCode breaking at the point my string was made into a NSURL. No logic as to why it was random even when I printed the url's they would look fine. As soon as I added the .addingPercentEncoding it was back working without any issues whatsoever.

    tl;dr For anyone reading this, try my above code and swap out "urlPath" for your own local string url.

    0 讨论(0)
提交回复
热议问题