C
C
casualfatboy2015-07-17 14:24:49
iOS
casualfatboy, 2015-07-17 14:24:49

Swift - how to see variables outside of a method (SwiftyJson)?

there is a code

override func viewDidLoad() {
        super.viewDidLoad()
        
        let url = NSURL(string: "https://api.whitehouse.gov/v1/petitions.json")
        var request = NSURLRequest(URL: url!)
        var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
        if data != nil {
        let hoge = JSON(data: data!)
        let count = hoge["results"][0]["body"]
        println(count)
        }
    }

it works great
. However, if I try to access the array from another method, it returns empty
override func viewDidLoad() {
        super.viewDidLoad()
        
        let url = NSURL(string: "https://api.whitehouse.gov/v1/petitions.json")
        var request = NSURLRequest(URL: url!)
        var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
        if data != nil {
        let hoge = JSON(data: data!)
        let count = hoge["results"][0]["body"]
        println(count)
        }
    }
    
    func res() {
        dump(hoge)
    }

dump returns empty in console
what am i doing wrong ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
one pavel, 2015-07-17
@onepavel

You have an array - a local object, make it a member of the controller

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question