简体   繁体   中英

Extra argument in call error - Swift

I'm trying to access a random value from a column of data in my Parse data controller. I created a query as follows, but keep getting an error that says, "Extra argument in call" where it says query.orderbyDescending(objectId1). If you aren't familiar with Parse, there is a column of String data called objectId and every row of data has an objectId. Choices, votes, votes2, optionName and optionName2 each also have their own column.

var query = PFQuery(className: "VoteCount")
    let objectId1: AnyObject! = voteCount1["objectId"]
    query.orderByDescending(objectId1) {
        (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
            let option1: AnyObject! = voteCount1["optionName"]
            let option2: AnyObject! = voteCount2["optionName2"]
            self.pollResults1.text = "\(votes)"
            self.showOption1.text = "\(option1)"
            self.showOption2.text = "\(option2)"
        }
    }

I've defined all my variables in the viewDidLoad method here:

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

What am I doing wrong? All I wish to do is to gather all of those variables that are defined in my viewDidLoad method from a random objectId in my Parse data controller. Is the orderByDescending query not the way to do it?

orderByDescending expects a string, whereas you are providing AnyObject! . Note that you don't have to declare a variable as implicitly unwrapped if you are supposed to assign a non-nil value. Just use a normal optional if it can be null, or a non optional otherwise.

Also, PFObject already exposes a objectId string property, so you don't have to use the subscript in order to retrieve it:

let objectId1 = voteCount1.objectId

However I notice a conceptual error in your code: you don't sort a query by a field value - you do it by field key (ie field name):

query.orderByDescending("objectId")

Moreover, I'm pretty sure that the objectId is automatically assigned a unique value when it is stored, so you do not have to assign a value to it, like you did in the last line of your code:

voteCount1["objectId"] = String()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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