【问题标题】:Parse Server // findObjectsInBackgroundWithBlock // Array解析服务器 // findObjectsInBackgroundWithBlock // 数组
【发布时间】:2023-03-12 18:24:01
【问题描述】:
Parse Server: 2.2.17 (self hosted)  
Parse-SDK-iOS-OSX: 1.14.2  
ParseUI-iOS Version: 1.2.0  
Xcode 7.3.1, Swift

我是 iOS 编程的新手(20 周),正在构建一个有趣和学习的应用程序。

我对 Parse 函数 findObjectsInBackgroundWithBlockArray 有疑问。完成我的函数后,会给我一个包含初始值的数组。在函数内部,我可以看到添加到数组中的值。

class UserOverview {

    var strings: [String] = ["1", "2", "3"]

    init() {
        userKcal()
    }

    func userKcal() -> [String] {
        let query = PFQuery(className: "StringClassInParse")
        query.whereKey("userPointer", equalTo: PFUser.currentUser()!)
        query.findObjectsInBackgroundWithBlock { (objects, error) in
            if let returnedObjects = objects {
                for object in returnedObjects {
                    let kcal = object["kcal"] as? String
                    self.strings.append(kcal!)
                }
            }
            print(self.strings) // output is ["1", "2", "3", "4", "5", "6"]
        }
        print(self.strings) // output is ["1", "2", "3"]
        return strings
    }
}

有什么想法吗? + 抱歉这个初学者问题。

【问题讨论】:

    标签: swift parse-platform swift2 parse-server pfquery


    【解决方案1】:

    您需要将closures 与您的函数一起使用,因为您正在使blockblock 将以async 的方式工作,因此您需要使用closure 声明您的函数,然后返回@987654326 @array 与 closure.

    func userKcal(completion: ([String]) -> ()) {
        let query = PFQuery(className: "StringClassInParse")
        query.whereKey("userPointer", equalTo: PFUser.currentUser()!)
        query.findObjectsInBackgroundWithBlock { (objects, error) in
            if let returnedObjects = objects {
                for object in returnedObjects {
                    let kcal = object["kcal"] as? String
                    self.strings.append(kcal!)
                }
            }
            completion(self.strings)
        }
    }
    

    现在这样调用这个函数

    self.userKcal { (strings) -> () in
         print(strings)
    }
    

    【讨论】:

    • 我看不懂代码(必须分析和学习),但它的工作原理就像一个魅力。非常感谢。
    • 如果您想了解更多有关此搜索的信息,例如 swift 中的关闭。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-07
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    相关资源
    最近更新 更多