【问题标题】:Issue with Swift ClosuresSwift 闭包问题
【发布时间】:2015-09-09 15:07:16
【问题描述】:

我在从闭包中检索数据时遇到问题。我正在调用名为getWallImages 的函数,它应该返回一个数组。我可以从闭包内打印数组的内容,但在闭包之外,数组是空的。

import Foundation
import Parse

class WallPostQuery {

    var result = [WallPost]()

    func getWallImages() -> [WallPost] { 
        let query = WallPost.query()!

        query.findObjectsInBackgroundWithBlock { objects, error in    
            if error == nil {     
                if let objects = objects as? [WallPost] {
                    self.result = objects
                    //This line will print the three PFObjects I have
                    println(self.result)
                }
            }
        }

        //this line prints [] ...empty array?
        println(result)
        return self.result
    }
}

问题

如何从闭包中获取值?

【问题讨论】:

    标签: swift parse-platform closures


    【解决方案1】:

    那是因为println(result)self.results = objects 之前执行。闭包是异步执行的,所以它会在之后执行。尝试制作一个使用可以从闭包中调用的结果的函数:

    var result = [WallPost]()
        func getWallImages() {
    
            let query = WallPost.query()!
    
            query.findObjectsInBackgroundWithBlock { objects, error in
    
                if error == nil {
    
                    if let objects = objects as? [WallPost] {
                        self.result = objects
                        //This line will print the three PFObjects I have
                        println(self.result)
                        self.useResults(self.result)
                    }
                }
            } 
        }
    
        func useResults(wallPosts: [WallPost]) {
            println(wallPosts)
        }
    
    }
    

    另一个解决您问题的方法是创建您自己的闭包,以便您可以从该函数返回它:

    var result = [WallPost]()
        func getWallImages(completion: (wallPosts: [WallPost]?) -> ()) {
    
            let query = WallPost.query()!
    
            query.findObjectsInBackgroundWithBlock { objects, error in
    
                if error == nil {
    
                    if let objects = objects as? [WallPost] {
                        self.result = objects
                        //This line will print the three PFObjects I have
                        println(self.result)
                        completion(wallPosts: self.result)
                    } else {
                        completion(wallPosts: nil)
                    }
                } else {
                    completion(wallPosts: nil)
                }
            } 
        }
    
        func useResults(wallPosts: [WallPost]) {
            println(wallPosts)
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      发生的事情是方法在闭包执行之前返回。

      从根本上说,您在管理异步回调的方式上遇到了问题。

      Asynchronous vs synchronous execution, what does it really mean?

      您需要创建一种在闭包中通知来电者的方法。您可以通过以下方式实现:要求您自己的闭包作为输入参数;使用委托模式;使用通知。

      https://codereview.stackexchange.com/questions/87016/swift-ios-call-back-functions

      每个都有其优点/缺点,这取决于您的具体情况。开始异步数据获取的最简单方法是传入您自己的闭包。如果需要,您可以从那里跳转到另一种模式,例如委托模式。

      【讨论】:

        【解决方案3】:

        我认为之前调用了 println(result) 的后者,因为 findObjectsInBackgroundWithBlock 顾名思义是在后台执行的。

        所以,您可以通过以下方式确认结果,

        import Foundation
        import Parse
        
        class WallPostQuery {
        
            var result = [WallPost]() {
                didSet {
                    println(result)
                }
            }
        
            func getWallImages() { 
                let query = WallPost.query()!
        
                query.findObjectsInBackgroundWithBlock { objects, error in    
                    if error == nil {     
                        if let objects = objects as? [WallPost] {
                            self.result = objects
                            //This line will print the three PFObjects I have
                            println(self.result)
                        }
                    }
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2017-06-30
          • 2020-03-08
          • 1970-01-01
          • 1970-01-01
          • 2014-08-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多