1

I want to save data from my parse class into strings. I use the following code actually for retrieving data from my parse class.

@IBAction func readAction(sender: UIButton) {

    var tagAutor = ""
    var tagText = ""

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

            // tagAutor = tag["username"]
            // tagText = tag["tagtext"]

        } else {
            println(error)
        }
    }
}

In the comments there is what I want to do, in my class called "Tags" there are two cols called "username" and "tagtext" I want to save them in the two string variables "tagAutor" and "tagText". The println(tag) is printing out the following:

My console output

How can I save the objects out of my query into this two string variables?

8
  • Isn't it just tag.tagText and tag.username? Commented May 11, 2015 at 18:48
  • What's wrong with the commented code? What are you going to use the for after the function completes? Commented May 11, 2015 at 18:50
  • No, I have found something here on another question like tagAutor = tag["username"] but there I'm getting an error "Can not assign a value of type 'AnyObject?' to value of type 'String'". Commented May 11, 2015 at 18:50
  • @Wain, the ID will be a variable so that it makes sense. After this method I print out the stuff in a textfield. Commented May 11, 2015 at 18:52
  • You just have to downcast it to String: tagAutor = tag["username"] as! String Commented May 11, 2015 at 18:53

1 Answer 1

1

Tell the compiler to convert the AnyObject to a String:

if let author = tag["username"] as String {
    tagAutor = author
}

And probably move the definition of tagAuthor so you can use I outside the function

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

6 Comments

There still an error called 'AnyObject?' is not convertible to 'String'.
How do you want to deal with optionals? You should probably make tagAuthor optional and test it before use. Or you could test and then set when extracting from the tag (does an empty string help you, probably not...)
In my class Tags username couldn't be empty, so I just want to save it in a String and go on.
Sorry, but this doesn't helped me out, it always gives me an error with this PFObject.
|

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.