在初始化之前由闭包捕获的变量 [英] Variable captured by closure before being initialized

查看:157
本文介绍了在初始化之前由闭包捕获的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将查询的结果数存储为整数,以便可以使用它来确定表中的行数。但是,我收到以下错误:在初始化之前,闭包捕获了变量'numberOfGames''在行 query.findObjectsInBackgroundWithBlock {

I'm trying to store the number of results from a query into an integer so that I can use it to determine the number of rows in a table. However, I'm getting the following error: Variable 'numberOfGames' captured by a closure before being initialized' on the line query.findObjectsInBackgroundWithBlock{.

我还遇到另一个错误在初始化之前使用的变量'numberOfGames' 返回numberOfGames

I also get another error Variable 'numberOfGames' used before being initialized on the line return numberOfGames.

以下是包含两个错误的函数:

Here's the function that contains the two errors:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        var user: PFUser!

        var numberOfGames: Int

        //...query code....removed to make it easier to read

        var query = PFQuery.orQueryWithSubqueries([userQuery, userQuery2, currentUserQuery, currentUserQuery2])
        query.findObjectsInBackgroundWithBlock{
            (results: [AnyObject]?, error: NSError?) -> Void in

            if error != nil {
                println(error)
            }

            if error == nil{

                if results != nil{
                    println(results)
                    numberOfGames = results!.count as Int
                }
            }
        }
        return numberOfGames
    }


推荐答案

使用前需要初始化变量放在闭包中:

You need to initialize the variable before use it inside a closure:

根据Apple文档


如果使用闭包来初始化属性,请记住实例的其余
在执行
关闭时尚未初始化。这意味着您无法从闭包内部访问任何其他
属性值,即使这些属性
具有默认值也是如此。您也不能使用隐式self属性,
或调用实例的任何方法。

If you use a closure to initialize a property, remember that the rest of the instance has not yet been initialized at the point that the closure is executed. This means that you cannot access any other property values from within your closure, even if those properties have default values. You also cannot use the implicit self property, or call any of the instance’s methods.

命令 var numberOfGames:Int 只需声明即可初始化即可,您可以使用 var numberOfGames = Int() var numberOfGames: Int = 0

The command var numberOfGames: Int just declare it to initialize you can use var numberOfGames = Int() or var numberOfGames:Int = 0

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        var user: PFUser!
        var numberOfGames:Int = 0
        var query = PFQuery.orQueryWithSubqueries([userQuery, userQuery2, currentUserQuery, currentUserQuery2])
        query.findObjectsInBackgroundWithBlock{
            (results: [AnyObject]?, error: NSError?) -> Void in
            if error != nil {
                println(error)
            }
            if error == nil{
                if results != nil{
                    println(results)
                    numberOfGames = results!.count as Int
                }
            }
        }
        return numberOfGames
    }

这篇关于在初始化之前由闭包捕获的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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