swift – 被初始化之前由闭包捕获的变量

swift – 被初始化之前由闭包捕获的变量,第1张

概述我正在尝试将查询中的结果数存储到一个整数中,以便我可以使用它来确定表中的行数.但是,我收到以下错误:在初始化之前由闭包捕获的变量’numberOfGames’在行上query.findObjectsInBackgroundWithBlock {. 我还得到另一个错误变量’numberOfGames’在被初始化之前在行上返回numberOfGames. 这是包含两个错误的函数: func table 我正在尝试将查询中的结果数存储到一个整数中,以便我可以使用它来确定表中的行数.但是,我收到以下错误:在初始化之前由闭包捕获的变量’numberOfGames’在行上query.findobjectsInBackgrounDWithBlock {.

我还得到另一个错误变量’numberOfGames’在被初始化之前在行上返回numberOfGames.

这是包含两个错误的函数:

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    }
您需要在使用它之前初始化变量:

As per apple documentation

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

func tableVIEw(tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int    {        var user: PFUser!        var numberOfGames:Int = 0        var query = PFquery.orqueryWithSubquerIEs([userquery,error: NSError?) -> VoID in            if error != nil {                println(error)            }            if error == nil{                if results != nil{                    println(results)                    numberOfGames = results!.count as Int                }            }        }        return numberOfGames    }
总结

以上是内存溢出为你收集整理的swift – 被初始化之前由闭包捕获的变量全部内容,希望文章能够帮你解决swift – 被初始化之前由闭包捕获的变量所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1041258.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-24
下一篇2022-05-24

发表评论

登录后才能评论

评论列表(0条)

    保存