【发布时间】:2017-01-21 21:42:52
【问题描述】:
我一直在尝试找出如何将来自 Alamofire 的 JSON 响应的一部分保存为变量以进行 if 语句,但似乎找不到如何做到这一点。
我编写了一段代码作为问题的示例,其中包含以下内容:
import UIKit
import Alamofire
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request("http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=44143256ee15e9c3f521ca062463dd8d").responseJSON { response in
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
}
}
我从 API 请求中得到以下响应:
JSON: {
base = stations;
clouds = {
all = 0;
};
cod = 200;
coord = {
lat = "51.51";
lon = "-0.13";
};
dt = 1485031800;
id = 2643743;
main = {
humidity = 83;
pressure = 1026;
temp = "270.54";
"temp_max" = "273.15";
"temp_min" = "267.15";
};
name = London;
sys = {
country = GB;
id = 5091;
message = "0.0038";
sunrise = 1484985141;
sunset = 1485016345;
type = 1;
};
visibility = 7000;
weather = (
{
description = haze;
icon = 50n;
id = 721;
main = Haze;
}
);
wind = {
speed = 1;
};
}
从这个 JSON 响应中,我想保存天气描述,以便我可以使用以下语句:
if var currentWeather = sunny {
return "Nice day!"
} else {
return "Uh-Oh, keep warm!"
}
如果有人能帮我解决这个问题,我将不胜感激!如果我必须使用 SwiftyJSON 来简化它,那很好,我已经为此苦苦挣扎了一段时间,需要弄清楚!
【问题讨论】:
标签: ios swift xcode swift3 alamofire