如何在给定函数外部的swift中访问变量 [英] How to access a variable in swift outside a given function

查看:130
本文介绍了如何在给定函数外部的swift中访问变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我试图在我的代码中的某个点访问我的变量ar1,但我不能。我正在尝试返回该值,但它在此时返回NULL。



这里有什么问题?



我试过以下代码:



我尝试过的事情:



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:

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()
}
    }

推荐答案

不确定您对ar1内部的期望?我看到它的方式实际上应该没有任何内容。



我建议你读一些块和关闭。



只是注意您正在运行的代码块将与您尝试返回ar1的线程分开执行,但在其中看不到任何内容。



如果你想让一些其他对象在你的代码块运行后有权访问结果那么你应该考虑使用通知或者带回调的协议。



希望有所帮助。
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.


这篇关于如何在给定函数外部的swift中访问变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆