0

I want to get a record from my parse.com class called "Tags" by ID. I want to retrieve data from the object "username" and "tagtext". Afterwords I want to save "username" to the String "username" and "tagtext" to the String "tagtext". My code looks like this, but I'm getting an error called 'AnyObject?' is not convertible to 'String' in the commented section:

var query = PFQuery(className:"Tags")
query.getObjectInBackgroundWithId("IsRTwW1dHY") {
    (gameScore: PFObject?, error: NSError?) -> Void in
    if error == nil && gameScore != nil {

         let username = gameScore["username"] as! String   // Error here
         let tagtext = gameScore["tagtext"] as! String     // Error here

    } else {

          println(error)

    }
}
2
  • possible duplicate of Error while retrieving data from parse.com Commented May 13, 2015 at 20:02
  • Thats a question from me, it's e different problem please don't mark it as a duplicate! Commented May 13, 2015 at 20:28

1 Answer 1

2

In Swift String is not an object. You need to cast NSString instead of String. Once you do you can then assign your NSString to a variable of type String.

edit following comment

You can do something like this:

if let username = gameScore["username"] as? NSString {
// If we get here, we know "username" exists, and we know that we
// got the type right. 
self.username = username 

}

Sign up to request clarification or add additional context in comments.

9 Comments

Do you mean something like this?: let username: String? = gameScore["username"] as! NSString as String
Please check edit. I'm on my mobile so can't check the code but it should work.
This one was a big issue when version 1.2 came out. Of the top of my head adding a question mark at the end of the second "as" will fix it. Did the code I give you work?
With the code I gave you or the one you wrote and fixed?
It must work unless parse is already returning a string. Try getting ride the exclamation point in your original code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.