Swift 范围界定问题 [英] Swift scoping issue

查看:51
本文介绍了Swift 范围界定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它从 parse.com 数据库中提取信息并将其传递到一个数组中.当我从 while 循环内部 println() 这个数组时,它打印得很好.当我尝试在 while 循环之外打印它时,它返回空.这是我的代码:

I have an app that is pulling info from a parse.com database and passing it into an array. When I println() this array from inside the while loop, it prints fine. When I try to print it outside the while loop, it returns empty. Here is my code:

var players = [String]()
var total = [String]()

var addTotal:AnyObject!
var addTotalFinal:Int!

var addPlayers:AnyObject!
var addPlayersFinal:Array<Int>!

@IBOutlet weak var tableView: UITableView!
var test: AnyObject!
override func viewDidLoad() {
    super.viewDidLoad()

     Parse.setApplicationId("KZ758LUTQZQ9Kl69mBDkv7BNLGHyXeKmqtFf7GmO", clientKey: "lOK5rg7wKeTRXZGV7MZBlQ5PTpNlGO0mkgtLkLgH")

    var query = PFQuery(className:"runningTotal")
    query.whereKey("total", notEqualTo: 0)
    query.findObjectsInBackgroundWithBlock
    {
        (objects: [AnyObject]!, error: NSError!) -> Void in
        if error == nil
        {

            var i = 0

            while i < objects.count
            {

            self.addTotal = objects[i]["total"]
            self.addTotalFinal = self.addTotal as Int

            self.addPlayers = objects[i]["players"]
            self.addPlayersFinal = self.addPlayers as Array

            self.players.append("\(self.addPlayersFinal)")
            self.total.append("\(self.addTotalFinal)")

                ++i
            }



        }
        else
        {

        }




    }

    navigationItem.title = "\(players)"



 }


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return players.count
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    var cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as UITableViewCell
    cell.textLabel.text = "\(players[indexPath.row]) - \(total[indexPath.row])"

    return cell

}

while 语句中的 println() 返回

The println() in the while statement returns

[[1, 11, 12, 24, 25]]
[2]

[[1, 11, 12, 24, 25], [1, 5, 24, 25, 31]]
[2, 4]

[[1, 11, 12, 24, 25], [1, 5, 24, 25, 31], [2, 12, 25, 15, 31]]
[2, 4, -1]

[[1, 11, 12, 24, 25], [1, 5, 24, 25, 31], [2, 12, 25, 15, 31], [24, 22, 25, 31, 20]]
[2, 4, -1, -6]

[[1, 11, 12, 24, 25], [1, 5, 24, 25, 31], [2, 12, 25, 15, 31], [24, 22, 25, 31, 20], [1, 2, 3, 4, 5]]
[2, 4, -1, -6, 5]

[[1, 11, 12, 24, 25], [1, 5, 24, 25, 31], [2, 12, 25, 15, 31], [24, 22, 25, 31, 20], [1, 2, 3, 4, 5], [1, 10, 11, 2, 12]]
[2, 4, -1, -6, 5, 1]

这是有道理的,因为有六次迭代.但是,如果我在 viewDidLoad 语句中而不是在 while 语句中打印,它会返回[][]

This makes sense, as there are six iterations. However, if I print inside the viewDidLoad statement instead of inside the while statement, it returns [][]

非常感谢任何帮助.

推荐答案

问题是你还没有理解线程/异步执行是如何工作的.让我们更简单.这是伪代码:

The problem is that you have not understood how threads / asynchronous execution works. Let's be simpler. This is pseudo-code:

func viewDidLoad {
    doStepOne() // you configure your query here
    doStepTwoWithAsynchBlock {
        doStepTwo() // your while loop is here
    }
    doStepThree() // your failing println is here
}

这按照doStepOne()doStepThree()doStepTwo() 的顺序执行.因此,如果 doStepThree() 尝试获取 doStepTwo() 设置的值,则它尚未准备好.

This executes in the order doStepOne(), doStepThree(), doStepTwo(). So if doStepThree() tries to get a value that doStepTwo() sets, it isn't ready yet.

异步块是发生的事情,在未来的某个时间 - 你所有的其他代码都完成了,包括整个其余的 viewDidLoad.

The asynch block is something that will happen, some time in the future - after all your other code has finished, including the whole of the rest of viewDidLoad.

这篇关于Swift 范围界定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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