1

Code sample is Swift 3.0 running in Xcode 8(β3).

This Weather Underground URL (when MyKey is replaced with a valid key) returns a JSON string containing the current weather at the given location... http://api.wunderground.com/api/MyKey/conditions/q/51.3276,-1.0022.json

The code below should do the same thing but instead throws an error ("The requested URL was not found on this server.", details at bottom).

Can anyone see what I'm missing in the code?

private let key = "MyKey" // Substitute a valid Weather Underground key here.

@IBAction func fetchWeather() {
    let session = URLSession.shared
    let url     = URL(fileURLWithPath: "http://api.wunderground.com/api/\(key)/conditions/q/51.3276,-1.0022.json")

    let handler = { (data: Data?, response: URLResponse?, error: NSError?) -> Void in
        guard let data = data, let response = response, error == nil else {
            print("\nError: \(error?.description)\n")
            return
        }
        print("\nData: \(data)\n")
        print("\nResponse: \(response)\n")
    }

    let task    = session.dataTask(with: url, completionHandler: handler)
    task.resume()
}

Error: Optional("Error Domain=NSURLErrorDomain Code=-1100 \"The requested URL was not found on this server.\" UserInfo={NSUnderlyingError=0x600000442520 {Error Domain=kCFErrorDomainCFNetwork Code=-1100 \"(null)\"}, NSErrorFailingURLStringKey=file:///http:/api.wunderground.com/api/MyKey/forecast/q/51.3276,-1.0022.json, NSErrorFailingURLKey=file:///http:/api.wunderground.com/api/MyKey/forecast/q/51.3276,-1.0022.json, NSLocalizedDescription=The requested URL was not found on this server.}")

Vince O'Sullivan
  • 2,611
  • 32
  • 45
  • 2
    `fileURLWithPath` => `URLWithString` when you construct your URL. Same issue as there: http://stackoverflow.com/questions/28267352/nsurlerrordomain-with-code-1100/28510196#28510196 – Larme Jul 28 '16 at 08:21
  • Well spotted! I was so convinced that I was already using `URL(string:...)` that I was blind to the fact that Xcode's code completion had actually inserted the wrong parameter. Make you comment an answer and I'll credit it. – Vince O'Sullivan Jul 28 '16 at 08:46
  • 1
    Since it's the same issue that in the previous link, it would be better to mark it as duplicate (to centralize answers, but keep also a new link to find it for user with the same issue). – Larme Jul 28 '16 at 08:48

0 Answers0