2

The API returns me the next:

{
    apparentTemperature = "37.08";
    cloudCover = 1;
    dewPoint = "31.44";
    humidity = "0.8";
    icon = cloudy;
    ozone = "305.37";
    precipIntensity = "0.0023";
    precipProbability = "0.06";
    precipType = rain;
    pressure = "1029.17";
    summary = Overcast;
    temperature = "37.08";
    time = 1450515600;
    windBearing = 196;
    windSpeed = "2.61";
},
{
    apparentTemperature = "39.22";
    cloudCover = 1;
    dewPoint = "33.13";
    humidity = "0.79";
    icon = cloudy;
    ozone = "304.86";
    precipIntensity = "0.0016";
    precipProbability = "0.03";
    precipType = rain;
    pressure = "1029.13";
    summary = Overcast;
    temperature = "39.22";
    time = 1450519200;
    windBearing = 203;
    windSpeed = "2.76";
},
{
    apparentTemperature = "41.17";
    cloudCover = 1;
    dewPoint = "34.77";
    humidity = "0.78";
    icon = cloudy;
    ozone = "303.99";
    precipIntensity = "0.0015";
    precipProbability = "0.03";
    precipType = rain;
    pressure = "1028.96";
    summary = Overcast;
    temperature = "41.17";
    time = 1450522800;
    windBearing = 213;
    windSpeed = "2.73";
},
{
    apparentTemperature = "42.47";
    cloudCover = "0.99";
    dewPoint = "36.04";
    humidity = "0.78";
    icon = cloudy;
    ozone = "302.97";
    precipIntensity = "0.0013";
    precipProbability = "0.02";
    precipType = rain;
    pressure = "1028.82";
    summary = Overcast;
    temperature = "42.47";
    time = 1450526400;
    windBearing = 215;
    windSpeed = "2.58";
});
    icon = "partly-cloudy-day";
    summary = "Mostly cloudy starting tomorrow morning.";
};
    latitude = "45.745138";
    longitude = "21.169898";
    offset = 2;
    timezone = "Europe/Bucharest";
})

My code is :

import UIKit
import Alamofire

class ViewController: UIViewController {

    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var webView: UIWebView!
    @IBOutlet weak var textView: UITextView!

    @IBAction func searchWeather(sender: UIButton) {
        loadData()

    }

    func loadData() {
        let city: String = textField.text!
        let api_key: String = "https://api.forecast.io/forecast/b33607b3d2611f604df5def613283a8a/"
        Alamofire.request(.GET, "\(api_key)45.745138,21.169898").responseJSON { response in
            print("\(response.result.value)")

        }

    }


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

I want to obtain some fields like temperature, humidity and pressure from the JSON response and put them in a UItextView into my App. How can I get only the three fields ?

Laffen
  • 2,753
  • 17
  • 29
Cristian
  • 117
  • 1
  • 1
  • 11
  • How did the JSON get that format? The JSON data you provide is invalid. – Laffen Dec 17 '15 at 12:57
  • Possible duplicate of [How to parse JSON response from Alamofire API in Swift?](http://stackoverflow.com/questions/26114831/how-to-parse-json-response-from-alamofire-api-in-swift) – Laffen Dec 17 '15 at 13:01

2 Answers2

2

Here an example for humidity (replace "humidity", with "temperature" and "pressure". Also see how I get inside your JSON, use this example to know how to navigate and get what you want

let daily = response.result.value!["daily"] as! NSDictionary
let data = daily["data"] as! NSArray
for obj in data {
  print (obj["humidity"])
}
hla
  • 258
  • 2
  • 10
  • Thanks man can you explain what exactly do the exclamation mark in general and after the as – Cristian Dec 17 '15 at 13:13
  • There is already a question and an answer about it on stack :) http://stackoverflow.com/questions/24018327/what-does-an-exclamation-mark-mean-in-the-swift-language – hla Dec 17 '15 at 13:15
1

In continuation of @hla's answer, forced unwraps/casts are to be avoided as much as possible. They will cause your application to crash if the value is nil/can't be casted. Please use optional binding for this kind of stuff.

The correct implementation would be:

if let daily = response.result.value?["daily"] as? NSDictionary, let data = daily["data"] as? NSArray {
    for obj in data {
        print (obj["humidity"])
    }
}
Cristik
  • 30,989
  • 25
  • 91
  • 127