简体   繁体   中英

How to add object of NSDictionary type to an NSMutableArray which contains objects of NSDictionaryResultType?

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];

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];

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.

Could you provide the other pieces of code you're referring to here? (Eg 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.

Could you provide the other pieces of code you're referring to here? (Eg 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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM