lundi 26 janvier 2015

How to use variables outside ViewDidLoad( ) - Swift

If I have declared a variable withing ViewDidLoad() in Xcode; is there a way to access that variable outside of ViewDidLoad( )?


I have retrieved and stored the JSON data from an API. I am able to access the variable 'hourlyInfo' within the ViewDidLoad( ), but not outside of it. Should I use global variables? Or am I in approaching the problem wrongly to begin with?



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

//getting the data from URL:
let mainAddress = NSURL(string: "https:.......")


//Now, getting the data syncronously by creating a session object::
let sharedSession = NSURLSession.sharedSession()
let downloadTask: NSURLSessionDownloadTask =
sharedSession.downloadTaskWithURL(mainAddress!, completionHandler: {
(location:NSURL!, response:NSURLResponse!, error: NSError!) -> Void in

if error == nil {
//Now, creating a dataObject for the task:
let dataObject = NSData(contentsOfURL: location)


//getting a formated dictionary of the data from URL:
var weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject!, options: nil, error: nil) as NSDictionary //added '!' to NSdata for now





var hourlyInfo = hourlyData(weatherDictionary: weatherDictionary)

println(hourlyInfo.hourPop1)//testing


}



})

downloadTask.resume()



Aucun commentaire:

Enregistrer un commentaire