【问题标题】:forecast wunderground api in Swift with json使用 json 在 Swift 中预测 wunderground api
【发布时间】:2015-01-11 03:14:21
【问题描述】:

我正在尝试从 Wunderground API 获取每小时预报,但我的代码返回此错误。 我对来自 Wunderground API 的研究项目的一天中特定小时的预测湿度感兴趣。

  let currenttt       = jsonResult["current_observation"] as NSDictionary
  let currentttv       = currenttt["display_location"] as NSDictionary
  let c: String!      = currentttv["city"] as NSString

此代码在 http://api.wunderground.com/api/0c5ad177d8c2e097/conditions/q/CA/San_Francisco.json 上完美运行 但是我需要添加什么才能获得不同时间的湿度。

我正在尝试获取图标的信息,我知道这取决于我需要添加的小时或其他值才能获得特定的“图标”。

让 urlAsString "http://api.wunderground.com/api/0c5ad177d8c2e097/forecast/q/CA/San_Francisco.json"

let w       = jsonResult["hourly_forecast"] as NSDictionary
let f       = w["FCTTIME"] as NSDictionary
let a: String!      = f["icon"] as NSString

错误是“操作无法完成。 (NSURLErrorDomain 错误 -1001。)致命错误:在展开可选值时意外发现 nil(lldb)“

和“线程 10:EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT,subcode=0xe7ffdefe)”

【问题讨论】:

  • 欢迎来到 SO!你遇到了什么错误?你能把它和你的问题一起发布吗?
  • 我正在尝试获取图标的信息,我知道这取决于我需要添加的小时数或其他值才能获得特定的“图标”。 let urlAsString "api.wunderground.com/api/0c5ad177d8c2e097/forecast/q/CA/…" let w = jsonResult["hourly_forecast"] as NSDictionary let f = w["FCTTIME"] as NSDictionary let a: String! = f["icon"] as NSString 错误是“操作无法完成。 (NSURLErrorDomain 错误 -1001。)致命错误:在展开可选值(lldb)时意外发现 nil“

标签: json api swift wunderground


【解决方案1】:
if let a = f["icon"] { print(a) }  

//prints 'mostly cloudy'

无需将 'a' 转换为 NSString。推断字符串类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    • 2017-12-08
    • 1970-01-01
    相关资源
    最近更新 更多