0

I have a button that pushes to a feed, I would like to record the name of the Feed Item in an array ("itemChosen") in my PFUser class every time the button is clicked:

@IBAction func buttonTapped(sender: UIButton) {

        PFUser.currentUser()!.addObject(feedItem.feedItemName, forKey: "itemChosen")
        PFUser.currentUser()!.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
            println(PFUser.currentUser()?.objectForKey("itemChosen"))
        })
}

I get the error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Operation is invalid after previous operation.'

If I use:

PFUser.currentUser()!.setObject(feedItem.feedItemName, forKey: "itemChosen")

I can save the name as a singular String, but I want to append it into an array. Why doesn't addObject work and how can I fix it?

1
  • The _User class is better for LOGIN AND SIGN UP purposes. It would be to create a new class into parse then when you are ready to save data you only associate their id with that data. Commented Aug 26, 2015 at 22:07

1 Answer 1

1

Try doing this:

@IBAction func buttonTapped(sender: UIButton) {
    if PFUser.currentUser()!["itemChosen"] == nil { PFUser.currentUser()!["itemChosen"] = [String]() }
    (PFUser.currentUser()!["itemChosen"] as! NSMutableArray).addObject(feedItem.feedItemName)
    PFUser.currentUser()!.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
        println(PFUser.currentUser()?.objectForKey("itemChosen"))
    })
}

The problem is that PFUser.currentUser!["itemChosen"] can be nil.

Edit: I found a way to do it using Xcode 6:

extension PFUser {
    var param: [String] {
        get {
            if let x = self["itemChosen"] as? [String] {
                return x
            } else {
                return []
            }
        }
        set(val) {
            self["itemChosen"] = val
        }
    }
}

@IBAction func buttonTapped(sender: UIButton) {
    PFUser.currentUser()!.param.append(feedItem.feedItemName)
    PFUser.currentUser()!.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
        println(PFUser.currentUser()?.objectForKey("itemChosen"))
    })
}
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for your answer.. I tried this and what I get is an error on line 2 'operand of postfix ! should have an optional type; type is () -> PFUser?' -- so I remove the !, and I get the following error '() -> PFUser does not have a member named 'subscript''
@SamYoungNY sorry, forgot to write an initializer. Edited now.
thanks but unfortunately I am getting the same errors
@SamYoungNY finally fixed. Now using NSMutableArray instead of [String]
Which version of Xcode are you using? Mine is v6.3.1 and I think that the next error I got may be due to this. After clicking button: Could not cast value of type 'Swift._SwiftDeferredNSArray' to 'NSMutableArray'. According to this post it may be Xcode related: stackoverflow.com/questions/30190848/…
|

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.