Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Essentially, I am trying to access my variable "ar1" at a certain point in my code but I cannot. I am trying to return the value too but it returns NULL at that point.

What is wrong here ?

I tried like the below code :

What I have tried:

Swift
import Foundation
import UIKit

class Web {
    var ar1 = [Double]()
    var ar2 = [Double]()

    func getData(str: String) -> [Double] {
    let request = NSMutableURLRequest(url: URL(string: str)!)
      httpGet(request as URLRequest!){
            (data, error) -> Void in
            if error != nil {
                print(error ?? "Error")
            } else {
                let delimiter = "\t"
                let lines:[String] = data.components(separatedBy:     CharacterSet.newlines) as [String]

                for line in lines {           
                    var values:[String] = []
                    if line != "" {
                        values = line.components(separatedBy: delimiter)
                         let str1 = (values[0])//FIRST COLUMN
                        let str2 = (values[1])//SECOND COLUMN
                       let db1 = NumberFormatter().number(from: str1)?.doubleValue
                        self.ar1.append(db1!)
                        let db2 = NumberFormatter().number(from: str2)?.doubleValue
                        self.ar2.append(db2!)
                    }
                }
            dump (self.ar1) // "ar1" accessible HERE (returns all elements)
        }
    }//end of request
        //BUT I WANT TO RETURN "ar1" HERE !
       // So that I can use it in my MAIN class
        dump (self.ar1) // "ar1" not accessible here (returns ZERO elements)
        return self.ar1
    }
        func httpGet(_ request: URLRequest!, callback: @escaping (String, String?) -> Void) {
            let session = URLSession.shared
            let task = session.dataTask(with: request, completionHandler: {
                (data, response, error) -> Void in
                if error != nil {
                callback("", error!.localizedDescription)
                } else {
                let result = NSString(data: data!, encoding:
                    String.Encoding.ascii.rawValue)!
                callback(result as String, nil)
                }
            })
            task.resume()
}
    }
Posted
Updated 28-Apr-17 4:52am
v2

1 solution

not sure what you expect there to be inside of ar1? the way that I see it there should actually be nothing in it.

I would recommend that you read up some more blocks and closure.

as just a note the code block that you are running will execute separately from the thread where you are attempting to return ar1 but see nothing in it.

if you want some other object to have access to the results after your code block is run then you should probably think of using a notification or maybe a protocol with a callback.

Hope that helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900