Reputation: 584
I am fetching data from Core Data with the following code-
NSManagedObjectContext *context=[[self appDelegate] managedObjectContext];
NSEntityDescription *entityDesc=[NSEntityDescription entityForName:@"Messages" inManagedObjectContext:context];
NSFetchRequest *request=[[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
[request setResultType:NSDictionaryResultType];
NSArray *objects=[context executeFetchRequest:request error:&error];
The function containing the above code returns me the NSArray of NSDictionaryType. On the view controller i stored them into a NSMutableArray *messages on viewdidload function. Now if new messages is received or sent , i want to store that new NSDictionary to messages. Doing so is generating error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x9740230'
Code to get data-
CoreDataHandler *handler=[[CoreDataHandler alloc] init];
NSMutableArray *messages=[[NSMutableArray alloc]initWithObjects: nil];
messages=(NSMutableArray *)[handler fetchMessages:[chatWithUser objectForKey:@"jidStr"]];
Code where i am adding object to messages-
NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
[m setObject:messageStr forKey:@"msg"];
[m setObject:@"you" forKey:@"sender"];
[m setObject:[NSString getCurrentTime] forKey:@"createdAt"];
[messages addObject:(NSDictionary *) m];
Upvotes: 1
Views: 429
Reputation: 1483
Could you provide the other pieces of code you're referring to here? (E.g. you say you store the messages obtained from the above fetch into an NSMutableArray. Show the code where it is declared a mutable array, where it is instanciated and the code where the fetch results are added). Judging by the error message you are trying to add an object to an NSArray, not to an NSMutableArray.
OK, so you are creating an NSMutableArray and storing its pointer in 'messages'. But then you assign a different pointer to 'messages'. You assign the non mutable result set to it. You should not do that. Instead of that just add the objects from the fetch to the messages object. Something like [messages addObjectsFromArray: fetchresults];
Upvotes: 0
Reputation: 1483
Could you provide the other pieces of code you're referring to here? (E.g. you say you store the messages obtained from the above fetch into an NSMutableArray. Show the code where it is declared a mutable array, where it is instanciated and the code where the fetch results are added). Judging by the error message you are trying to add an object to an NSArray, not to an NSMutableArray.
Upvotes: 0
Reputation: 25459
You should use NSMutableArray instead on NSArray, NSArray doesn't contain method addObject. You can create mutable copy like:
NSMutableArray *objects=[[context executeFetchRequest:request error:&error] mutableCopy;
Now you are free to call addObject method on that object.
Upvotes: 1
Reputation: 7466
Just because you declare a dog to be a cat, it doesn't mean it will "miau" suddenly. It is still a dog, only in cat's clothing. Now on a serious note. The problem is that you are trying to put things into an NSArray which is immutable. Once an immutable array is created, you can't change it's content.
You might try this
NSMutableArray *objects=[[context executeFetchRequest:request error:&error] mutable copy];
Upvotes: 1