0

enter image description here

Recently note that wherekey on PFQuery does not work. I tried 3 approaches but all failed. I am using parse-library-1.7.2 on Xcode 6.3.

Error for approach1: Cannot invoke 'whereKey' with an argument list of type '(String, AnyObject)'

Error for approach2: 'String?'is not convertible to 'StringLiteralConvertible'

Error for approach3: 'AnyObject?' is not convertible to 'String'

Code is as below. Anyone can help with this please? Thanks in advance.

  // Define the query that will provide the data for the table view
class MyController: PFQueryTableViewController {
override func queryForTable() -> PFQuery {
    var query = PFQuery(className: "Ticket")
    //Approach 1
    query.whereKey(sellerIdKey, equalTo: currentPhoneUser["objectId"]!)

    //Approach 2
    if let sellerId = currentPhoneUser["objectId"] as? String {
        query.whereKey(sellerIdKey, equalTo: sellerId)
        query.orderByDescending("createdAt")
        return query
    } else {
        fatalError("Can't get Object Id of this user")
    }

    //Approach 3
    if let sellerId = currentPhoneUser["objectId"] as! String {
        query.whereKey(sellerIdKey, equalTo: sellerId)
        query.orderByDescending("createdAt")
        return query
    } else {
        fatalError("Can't get Object Id of this user")
    }
}

}
Tristan.Liu
  • 176
  • 1
  • 7

1 Answers1

1

Turns out to be a swift 1.2 issue. How to cast AnyObject? to String in Swift 1.2

Below is my final solution that compiles:

override func queryForTable() -> PFQuery {
    var query = PFQuery(className: "Ticket")
    let objectId = currentPhoneUser?["objectId"] as? String
    if let constObjectId = objectId {
        query.whereKey(sellerIdKey, equalTo: constObjectId)
    } else {
        fatalError("Cannot find Object Id of current user")
    }
    return query
}
Community
  • 1
  • 1
Tristan.Liu
  • 176
  • 1
  • 7