简体   繁体   English

没有与查询匹配的结果

[英]No results matched the query

I'm not sure if this is a matter of the IBAction below the didReceiveMemoryWarning not calling the same variables as the query below viewDidLoad method, or some other issue. 我不确定这是否是didReceiveMemoryWarning下的IBAction是否未调用与viewDidLoad方法下的查询相同的变量或其他问题。 But when I run this code, I get an error that says "no results matched the query" and "the operation couldn't be completed. Parse error 101." 但是,当我运行此代码时,出现一个错误,提示“没有结果与查询匹配”,并且“操作无法完成。解析错误101。” Could anyone tell me why that is? 谁能告诉我为什么呢?

    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)"
        }
        }
    }
}

What I'm trying to do is call the values for the variables "votes" "votes2" "option1" and "option2" from a random row on my Parse database. 我想做的是从我的Parse数据库的随机行中调用变量“ votes”,“ votes2”,“ option1”和“ option2”的值。 I've created a column of Int data called "voteNumber" that each contains a number 1 to 1000 and I believe by calling a row with a random number I should be able to call all the appropriate data from a random row. 我创建了一个名为“ voteNumber”的Int数据列,每个列包含一个1到1000的数字,并且我相信通过调用具有随机数的行,我应该能够从随机行中调用所有适当的数据。 I'm just not sure if I'm bridging the query within the viewDidLoad method with the IBActions underneath the closing of the didReceiveMemoryWarning method. 我只是不确定是否要在didDceiveMemoryWarning方法关闭下方的IBActions与viewDidLoad方法内桥接查询。

Looking at the definition, equalTo: expects an id which translates to AnyObject in Swift. 查看定义, equalTo:需要一个id ,该id可以转换为Swift中的AnyObject In both Swift and Objective-C you would need to convert the number to a NSNumber object to get the code to work. 在Swift和Objective-C中,您都需要将数字转换为NSNumber对象,以使代码正常工作。

Approach 1: Manual conversion 方法1:手动转换

The first approach is to simply create a new NSNumber wrapping the value: 第一种方法是简单地创建一个包装值的新NSNumber

Objective-C: Objective-C的:

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

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

Swift: 迅速:

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

Approach 2: Objective-C bridging 方法2:Objective-C桥接

Certain types in Swift qualify for automatic bridging to Objective-C types. Swift中的某些类型可以自动桥接到Objective-C类型。 While UInt32 doesn't qualify, it can safely upcast to UInt which does: 尽管UInt32不符合条件,但它可以安全地向上转换为UInt ,它可以:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM