- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 https://developer.forecast.io/ 获取某些位置的天气接口(interface)。API调用格式为https://api.forecast.io/forecast/APIKEY/LATITUDE,LONGITUDE
我可以从一个位置获得响应,但是当我尝试更改位置并再次使用 NSURL 获取天气时,NSURL 返回 Nil。为什么会这样以及如何处理?
谁能帮帮我?谢谢。
func getCurrentWeatherData() -> Void {
let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
var forecastURL = NSURL(string: "36.107728,-112.113040", relativeToURL: baseURL)
let sharedSession = NSURLSession.sharedSession()
let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: {
(location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in
//var urlContents = NSString(contentsOfURL: location, encoding: NSUTF8StringEncoding, error: nil)
if (error == nil) {
let dataObject = NSData(contentsOfURL: location)
let weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject!, options: nil, error: nil) as NSDictionary
let currentWeather = Current(weatherDictionary: weatherDictionary)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.iconView.image = currentWeather.icon!
self.currentTimeLabel.text = "At \(currentWeather.currentTime!) it is"
self.temperatureLabel.text = "\(Double(currentWeather.temperature-32) * 0.56)"
self.summaryLabel.text = "\(currentWeather.summary)"
self.refreshActivityIndicator.stopAnimating()
self.refreshActivityIndicator.hidden = true
self.refreshButton.hidden = false
})
} else {
let networkIssueController = UIAlertController(title: "Error", message:"Unable to load data. Connectivity error!", preferredStyle: .Alert)
let okButton = UIAlertAction(title: "OK", style: .Default, handler:nil)
networkIssueController.addAction(okButton)
let cancelButton = UIAlertAction(title:"Cancel", style: .Cancel, handler:nil)
networkIssueController.addAction(cancelButton)
self.presentViewController(networkIssueController, animated: true, completion: nil)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.refreshActivityIndicator.stopAnimating()
self.refreshActivityIndicator.hidden = true
self.refreshButton.hidden = false
})
}
})
downloadTask.resume()
var forecastURL2 = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/36.861941, -111.374420")
let sharedSession2 = NSURLSession.sharedSession()
**//forcastURL2 returns Nil**
let downloadTask2: NSURLSessionDownloadTask = sharedSession2.downloadTaskWithURL(forecastURL2!, completionHandler: {
(location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in
//var urlContents = NSString(contentsOfURL: location, encoding: NSUTF8StringEncoding, error: nil)
if (error == nil) {
let dataObject = NSData(contentsOfURL: location)
let weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject!, options: nil, error: nil) as NSDictionary
let currentWeather = Current(weatherDictionary: weatherDictionary)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.iconView2.image = currentWeather.icon!
self.temperatureLabel2.text = "\(Double(currentWeather.temperature-32) * 0.56)"
self.summaryLabel.text = "\(currentWeather.summary)"
self.refreshActivityIndicator.stopAnimating()
self.refreshActivityIndicator.hidden = true
self.refreshButton.hidden = false
})
} else {
let networkIssueController = UIAlertController(title: "Error", message:"Unable to load data. Connectivity error!", preferredStyle: .Alert)
let okButton = UIAlertAction(title: "OK", style: .Default, handler:nil)
networkIssueController.addAction(okButton)
let cancelButton = UIAlertAction(title:"Cancel", style: .Cancel, handler:nil)
networkIssueController.addAction(cancelButton)
self.presentViewController(networkIssueController, animated: true, completion: nil)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.refreshActivityIndicator.stopAnimating()
self.refreshActivityIndicator.hidden = true
self.refreshButton.hidden = false
})
}
})
downloadTask2.resume()
}
最佳答案
您使用的 URL ("https://api.forecast.io/forecast/\(apiKey)/36.861941, -111.374420"
) 应该格式正确,并且根据 Apple 的说法,符合RFC2396 .如果不是 NSURL
将返回 nil
。
您使用的网址不正确。例如空格应该使用 "%20"
进行转义。对于您的情况,我认为您可以删除空格。
关于ios - 尝试重用 NSURL 时获取 Nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27613466/
方法 -isFileReferenceURL 是在 NSURL 类上定义的,但我不知道这个方法是什么。我认为这决定了 URL 是否指向实际文件,但事实并非如此。即使文件存在,它也会返回NO。手册上说只
给定一个本地 URL 地址,例如 index.html 现在我需要使用UIWebView在iPad中加载它。我按照以下步骤操作: 创建 NSURL NSURL *url = [NSURL fileUR
我正在尝试将 NSString(文档目录中文件的路径)转换为 NSURL,但 NSURL 始终为空。这是我的代码: NSURL *urlToPDF = [NSURL URLWithString:app
我有一个将属性定义为 NSURL 的对象: @interface fcpElement : NSObject @property (copy) NSString* elementName; @prop
我正在尝试使用 swift 和 firebase 构建聊天应用程序并收到此错误:下面是代码:我仍然不明白哪里出了问题!!我也在使用 JSQMessageViewController。 func sen
我尝试将 deviceToken 附加到 webView loadRequest 之前的 URL,以便我可以使用 GET 值PHP 脚本。我的问题是,保存 deviceToken 值的函数 didFa
我有 2 个 [NSURL] 数组,它们具有相同的元素,但顺序不同。我正在尝试使用 array1 的索引路径来获取成员,并在另一个数组中检测成员的索引路径。 我不知道我是否可以检测到成员在 NSURL
我有一个文件(假设是一个 PDF)存储在我的 macOS Cocoa 应用程序的磁盘上。我有包含此文件路径的 NSURL。 我目前正在使用 NSView 向用户显示此文件存在,其中包含一个显示文件名的
我为 Swift 2 升级了我的代码,这里出现错误: 无法使用 (fileURLWithPath: NSURL) 类型的参数列表调用类型 NSURL 的初始化程序 代码如下: let dirP
我正在尝试将 NSURL * _Nullable 转换为不可为空。你们有没有人知道我该怎么做? 这里是一些上下文: 我正在尝试使用 ios-oauthconsumer ,我正在尝试初始化一些对象: s
我最近在比较两个 NSURL 并将一个 NSURL 与 NSString(这是一个 URL 地址)进行比较时遇到了一个问题,情况是我从某个地方得到了一个 NSURLRequest,我可能知道也可能不知
我必须解析一个 JSON API 的返回,我已将 URL 转换为一个字符串,然后我将它传递给 NSURL,但在 NSLog 中它显示为 nil。这是我的代码, NSString *urlstring=
我有一个名为 downloadedPhotoURLs 的变量,其类型为 [NSURL]?。 我正在尝试分配返回类型 [NSURL] 的函数的结果(非可选)。 我在分配时解开变量downloadedPh
我有一个网址 http://www.hdwallpapers.in/walls/honda_v4_concept_widescreen_bike-wide.jpg 我要提取的文件名为“honda_v4
我有一个 NSURL: 服务器调用?x=a&y=b&z=c 获取 y 值最快、最有效的方法是什么? 谢谢 最佳答案 更新: 自 2010 年撰写本文以来,Apple 似乎已经为此目的发布了一套工具。请
我在 SO 上看到了很多关于 NSURL 和 NSString 之间转换的问题。它们都涉及使用 NSString *path = [myURL AbsoluteString]; 或 NSString
-[NSURL startAccessingSecurityScopedResource] 状态的文档: You must balance every call to the startAccessi
我在 SO 上看到了很多关于 NSURL 和 NSString 之间转换的问题。它们都涉及使用 NSString *path = [myURL AbsoluteString]; 或 NSString
我在 SO 上看到了很多关于 NSURL 和 NSString 之间转换的问题。它们都涉及使用 NSString *path = [myURL AbsoluteString]; 或 NSString
我在 SO 上看到了很多关于 NSURL 和 NSString 之间转换的问题。它们都涉及使用 NSString *path = [myURL AbsoluteString]; 或 NSString
我是一名优秀的程序员,十分优秀!