I am trying to create a weather application, and in order to do that I need to parse out JSON. This works, (which I know because I tested it by printing), but I cannot change the value of the data I get to an already initialized global variable. A similar question was asked before, but even using the provided answers, I was not able to solve my own problem.
The following lines are where I initialize my global variables:
var currentTemperature = 88
var currentTime = 789
And the following Lines are where I parse the JSON and test by printing in my viewDidLoad function:
let url = URL(string: "https://api.darksky.net/forecast/c269e5928cbdadde5e9d4040a5bd4833/42.1784,-87.9979")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil {
print("error")
}
else {
if let content = data {
do {
let json = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
if let jsonCurrently = json["currently"] as? NSDictionary {
if let jsonCurrentTime = jsonCurrently["time"] {
currentTime = jsonCurrentTime as! Int
print(currentTime)
}
if let jsonCurrentTemperature = jsonCurrently["temperature"] {
currentTemperature = jsonCurrentTemperature as! Int
print(currentTemperature)
}
}
} catch {
}
}
}
}
task.resume()
I use the global variables when setting the text of a label in a different class: (however, only the initial value I set to the variable shows up, not the one from the parsed JSON)
let currentTemperatureLabel: UILabel = {
//label of the current temperature
let label = UILabel()
label.text = String(currentTemperature) + "°"
label.textColor = UIColor(red: 150/255, green: 15/255, blue: 15/255, alpha: 0.8)
label.textAlignment = NSTextAlignment.center
label.font = UIFont(name: "Damascus", size: 130)
label.font = UIFont.systemFont(ofSize: 130, weight: UIFontWeightLight)
return label
}()
The JSON example request can be found here: https://darksky.net/dev/docs#api-request-types
No matter what I do, I am not able to use the data from the JSON when I attempt to access the two global variables mentioned before.
var currentTemperature : Double = 88
var currentTime = 789
//...
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://api.darksky.net/forecast/c269e5928cbdadde5e9d4040a5bd4833/42.1784,-87.9979")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil {
print("error")
}
else {
if let content = data {
do {
let json = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
if let jsonCurrently = json["currently"] as? NSDictionary {
if let jsonCurrentTime = jsonCurrently["time"] as? Int {
currentTime = jsonCurrentTime
print(currentTime)
}
if let jsonCurrentTemperature = jsonCurrently["temperature"] as? Double {
currentTemperature = jsonCurrentTemperature
print(currentTemperature)
}
}
} catch {
}
}
}
}
task.resume()
}
I did these edits above to your code:
currentTemperature
to be Double (Look at your JSON response to see what kind of response you get and what kind of data type it can be) EDIT: Updated answer based on your comment about the global variables not being part of the class
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.