【问题标题】:No results matched the query没有与查询匹配的结果
【发布时间】:2014-11-26 22:35:34
【问题描述】:

我不确定这是否与 didReceiveMemoryWarning 下面的 IBAction 没有调用与 viewDidLoad 方法下面的查询相同的变量有关,还是其他问题。但是,当我运行此代码时,我收到一条错误消息,提示“没有与查询匹配的结果”和“操作无法完成。解析错误 101”。谁能告诉我这是为什么?

    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    var voteCount1 = PFObject(className: "VoteCount")
    voteCount1["choices"] = 2
    voteCount1["votes"] = Int()
    voteCount1["votes2"] = Int()
    voteCount1["optionName"] = String()
    voteCount1["optionName2"] = String()
    voteCount1["objectId"] = String()

    var voteCount2 = PFObject(className: "VoteCount2")
    voteCount2["choices"] = 3
    voteCount2["votes"] = Int()
    voteCount2["votes2"] = Int()
    voteCount2["votes3"] = Int()
    voteCount2["optionName"] = String()
    voteCount2["optionName2"] = String()
    voteCount2["optionName3"] = String()

    var voteCount3 = PFObject(className: "VoteCount3")
    voteCount3["choices"] = 4
    voteCount3["votes"] = Int()
    voteCount3["votes2"] = Int()
    voteCount3["votes3"] = Int()
    voteCount3["votes4"] = Int()
    voteCount3["optionName"] = String()
    voteCount3["optionName2"] = String()
    voteCount3["optionName3"] = String()
    voteCount3["optionName4"] = String()

    var query = PFQuery(className: "VoteCount")
    query.countObjectsInBackgroundWithBlock {
        (count: Int32, error: NSError!) -> Void in
        if error == nil {
            let randNumber = arc4random_uniform(UInt32(count))
            query.whereKey("voteNumber", equalTo: NSNumber(unsignedInt:randNumber))
            query.getFirstObjectInBackgroundWithBlock {
                (voteCount1: PFObject!, error: NSError!) -> Void in
                if error != nil {
                    NSLog("%@", error)
                } else {
                    let votes = voteCount1["votes"] as Int
                    let votes2 = voteCount1["votes2"] as Int
                    let option1 = voteCount1["optionName"] as String
                    let option2 = voteCount1["optionName2"] as String
                    self.showOption1.text = "\(option1)"
                    self.showOption2.text = "\(option2)"
                }

            }
        } else {
            println("error \(error)")
        }
        }


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}




@IBOutlet weak var pollResults: UILabel!

@IBAction func addVote1(sender: AnyObject) {
    var query = PFQuery(className: "VoteCount")
    query.getObjectInBackgroundWithId("objectId") {
        (voteCount1: PFObject!, error: NSError!) -> Void in
        if error != nil {
            NSLog("%@", error)
        } else {
            voteCount1.incrementKey("votes")
            voteCount1.saveInBackgroundWithTarget(nil, selector: nil)
            let votes = voteCount1["votes"] as Int
            let votes2 = voteCount1["votes2"] as Int
            self.pollResults.text = "\(votes)   \(votes2)"
        }
        }
}

@IBOutlet weak var pollResults2: UILabel!

@IBAction func addVote2(sender: AnyObject) {
    var query = PFQuery(className: "VoteCount")
    query.getObjectInBackgroundWithId("objectId") {
        (voteCount1: PFObject!, error: NSError!) -> Void in
        if error != nil {
            NSLog("%@", error)
        } else {
            voteCount1.incrementKey("votes2")
            voteCount1.saveInBackgroundWithTarget(nil, selector: nil)
            let votes = voteCount1["votes"] as Int
            let votes2 = voteCount1["votes2"] as Int
            self.pollResults2.text = "\(votes)   \(votes2)"
        }
        }
    }
}

我要做的是从我的 Parse 数据库的随机行中调用变量“votes”、“votes2”、“option1”和“option2”的值。我创建了一个名为“voteNumber”的 Int 数据列,每个包含一个 1 到 1000 的数字,我相信通过使用随机数调用一行,我应该能够从随机行调用所有适当的数据。我只是不确定我是否将 viewDidLoad 方法中的查询与在 didReceiveMemoryWarning 方法关闭下方的 IBAction 桥接。

【问题讨论】:

    标签: ios swift parse-platform


    【解决方案1】:

    查看定义,equalTo: 期望 id 在 Swift 中转换为 AnyObject。在 Swift 和 Objective-C 中,您需要将数字转换为 NSNumber 对象才能使代码正常工作。

    方法一:手动转换

    第一种方法是简单地创建一个新的NSNumber 包装值:

    目标-C:

    // Option 1:
    [query whereKey:@"voteNumber" equalTo: @(randNumber)];
    
    // Option 2:
    [query whereKey:@"voteNumber" equalTo: [NSNumber numberWithUnsignedInt:randNumber]];
    

    斯威夫特:

    query.whereKey("voteNumber", equalTo: NSNumber(unsignedInt:randNumber))
    

    方法 2:Objective-C 桥接

    Swift 中的某些类型符合 automatic bridging 到 Objective-C 类型的条件。虽然UInt32 不符合条件,但它可以安全地向上转换为UInt,它符合条件:

    let randNumber = UInt(arc4random_uniform(UInt32(count)))
    query.whereKey("voteNumber", equalTo: randNumber)
    

    【讨论】:

    • 非常感谢您的回复。难道不能在 didReceiveMemoryWarning 方法内外都进行查询来调用和更新来自同一源的数据吗?我刚刚更新了我上面的问题。
    • @hamzawesome 您应该将其作为一个新问题发布。这与原来的问题完全不同。
    • 我不能,我被禁止在一个月内发布更多问题,所以这是我唯一的选择,哈哈。当然,这就是我将代码更新为的内容。
    • @hamzawesome 在接受答案后更改您的问题不会有任何帮助。它稀释了网站的价值,使人们不太可能回答您的问题。而是阅读this post on how to get unblocked
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-05
    • 1970-01-01
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多