【问题标题】:Testing pointer equality with PFQuery, wrong syntax用 PFQuery 测试指针相等,语法错误
【发布时间】:2018-04-12 02:52:17
【问题描述】:

这里有一些 Parse-Server 相关的 swift 代码不起作用,可能只是因为一些明显的语法错误。

如果有人能指出问题,我将非常高兴和感激。

func getSentenceTranslations(_ sentences:[PFObject]) {
    let query = PFQuery(className: "TranslationsList")
    query.whereKey("sentence", containedIn: sentences)
    query.addAscendingOrder("order")
    query.findObjectsInBackground {
        [weak self] (objects: [PFObject]?, error: Error?) in
        if error != nil {
            print("Error in \(#function)\n" + (error?.localizedDescription)!)
            return
        }

        // The line below prints the expected number (> 0).
        print("\(objects?.count ?? 99) translations found.")

        for sentence in sentences {
            for translation in objects! {
                // The following does not work!!
                if (translation.value(forKey: "sentence") as! PFObject) == sentence {
                    print("FTF: \(sentence.value(forKey: "sentence")!)") // MISSING!!
                }
            }
        }
    }
}

但事实是,如果我在正确的时刻停止调试器,我可以看到注释行上应该有一个命中 (MISSING!!)。以下是调试器显示的内容:

(lldb) p sentence.debugDescription
(String) $R31 = "<SentencesList: 0x1073j7450, objectId: krxX9WsZuxK, localId: (null)> {\n    order = 3;\n    owner = Ht8AbcR543;\n    sentence = \"Hello big world of things.\";\n}"
(lldb) p translation.debugDescription
(String) $R32 = "<TranslationsList: 0x10739f0e0, objectId: FoBdjoPF1n, localId: (null)> {\n    order = 0;\n    owner = Ht8AbcR543;\n    sentence = \"<SentencesList: 0x1073aa8c0, objectId: krxX9WsZuxK, localId: (null)>\";\n    translation = \"Die Welt von immer!\";\n}"
(lldb) 

我们可以看到值 krxX9WsZuxK 在句子 (objectId) 和翻译 (sentence field) 上都可以找到,所以我希望有这样一行:

FTF: .......

要打印,这不会发生。所以我怀疑这行有错误:

if (translation.value(forKey: "sentence") as! PFObject) == sentence {

我尝试了各种其他变体都失败了。

【问题讨论】:

    标签: ios swift parse-server pfquery


    【解决方案1】:

    您好,您应该使用以下方法在查询响应中包含“句子”:

    query.includeKey("sentence") query.findObjectsInBackground { ...

    它将帮助查询将句子作为 PFObject 包含到结果中。这具有类似于连接的效果。您可以使用点表示法来指定包含对象中的哪些字段也被获取。

    【讨论】:

      【解决方案2】:

      在搜索和尝试了很多东西之后,这是有效的:

      我需要替换这一行:

        if (translation.value(forKey: "sentence") as! PFObject) == sentence {
      

      通过这个:

        if (translation.value(forKey: "sentence") as! PFObject).value(forKey: "objectId")  as! String ==
            sentence.value(forKey: "objectId") as! String {
      

      这很有道理,但我希望能有某种速写。 无论如何,我希望它在某些时候对其他人也有用。

      【讨论】:

        猜你喜欢
        • 2021-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-24
        • 2021-11-28
        • 1970-01-01
        • 2020-01-18
        • 1970-01-01
        相关资源
        最近更新 更多