user3233721
user3233721

Reputation: 31

How to solve mutating method sent to immutable object error

Im currently trying to design a twitter app.

However, I'm getting a mutating method sent to immutable object error.

in the appDelegate is the following:

@property (strong, nonatomic) NSMutableArray *tweets;

in my table view, when trying to add an object to the array i have

appDelegate.tweets = [responseObject objectForKey:@"tweets"];
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
Tweet *newTweet = appDelegate.tweets.lastObject;
NSLog(@"Tweets: %@", newTweet);
[appDelegate.tweets addObject:newTweet];

newTweet when logged displays:

Tweets: {
isdeleted = 0;
"time_stamp" = "2015-04-07 13:42:34";
tweet = "forgot the .text. dont worry people, its all under control.";
"tweet_id" = 37;
username = brett}

however i get an error when trying to add it to my array. Any idea on how to solve this?

Upvotes: 0

Views: 67

Answers (1)

Dare
Dare

Reputation: 2587

By error do you mean crash? The response object probably isn't returning a mutable array.

appDelegate.tweets = [(NSArray*)[responseObject objectForKey:@"tweets"]mutableCopy];

On a side note, do you really want this to live in the AppDelegate? This looks like it belongs in a data model.

Upvotes: 1

Related Questions