没有与查询匹配的结果

时间:2014-11-26 22:35:34

标签: ios swift parse-platform

我不确定这是否是在didReceiveMemoryWarning下面的IBAction问题,而不是调用与viewDidLoad方法下面的查询相同的变量,或者其他一些问题。但是当我运行这段代码时,我收到的错误是“#34;没有结果与查询匹配”#34;并且"操作无法完成。解析错误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)"
        }
        }
    }
}

我要做的是调用变量的值"投票" " votes2" "选项1"和"选项2"从我的Parse数据库中的随机行。我创建了一个名为" voteNumber"的Int数据列。每个包含一个1到1000的数字,我相信通过调用一个随机数的行我应该能够从随机行调用所有适当的数据。我只是不确定我是否将viewDidLoad方法中的查询与didReceiveMemoryWarning方法结束时的IBActions联系起来。

1 个答案:

答案 0 :(得分:1)

查看定义时,equalTo:需要id转换为Swift中的AnyObject。在Swift和Objective-C中,您需要将数字转换为NSNumber对象以使代码生效。

方法1:手动转换

第一种方法是简单地创建一个包含值的新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)