简体   繁体   English

如何将NSDictionary类型的对象添加到包含NSDictionaryResultType对象的NSMutableArray中?

[英]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- 我正在使用以下代码从Core Data中获取数据-

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. 包含以上代码的函数向我返回了NSDictionaryType的NSArray。 On the view controller i stored them into a NSMutableArray *messages on viewdidload function. 在视图控制器上,我将它们存储到viewdidload函数中的NSMutableArray *消息中。 Now if new messages is received or sent , i want to store that new NSDictionary to messages. 现在,如果收到或发送了新消息,我想将新的NSDictionary存储到消息中。 Doing so is generating error: 这样做会产生错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x9740230' 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“-[__ NSArrayI addObject:]:无法识别的选择器已发送到实例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. 问题是您正在尝试将东西放入不可变的NSArray中。 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. 您应该在NSArray上使用NSMutableArray,NSArray不包含方法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. 现在,您可以随意在该对象上调用addObject方法。

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). (例如,您说您将从上述获取中获得的消息存储在NSMutableArray中。在声明为可变数组的地方显示代码,在实例化该代码,并在获取结果中添加代码。) Judging by the error message you are trying to add an object to an NSArray, not to an NSMutableArray. 从错误消息来看,您正在尝试将对象添加到NSArray,而不是添加到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). (例如,您说您将从上述获取中获得的消息存储在NSMutableArray中。在声明为可变数组的地方显示代码,在实例化该代码,并在获取结果中添加代码。) Judging by the error message you are trying to add an object to an NSArray, not to an NSMutableArray. 从错误消息来看,您正在尝试将对象添加到NSArray,而不是添加到NSMutableArray。

OK, so you are creating an NSMutableArray and storing its pointer in 'messages'. 好的,因此您正在创建一个NSMutableArray并将其指针存储在“消息”中。 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]; 像[messages addObjectsFromArray:fetchresults]之类的东西;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将对象添加到作为NSDictionary元素的NSMutableArray - Add Object to an NSMutableArray that is an element of an NSDictionary 如何对包含Modal类型对象的NSMutableArray进行排序? - How to sort NSMutableArray that contains object of type Modal? 如何更改包含日期的NSMutablearray对象的日期格式? - How to change the date formate of objects of NSMutablearray which contains dates? NSMutableArray中包含对象的组元素 - Group element in NSMutableArray which contains object 将自定义对象序列化为包含NSMutableArray的JSON - Serialize custom object to JSON which contains NSMutableArray 将相同的硬编码对象添加到NSDictionary中以获取NSMutableArray的计数 - Add same hard coded object to NSDictionary for the count of an NSMutableArray 在包含视图集的TableView中添加NSMutableArray - Add NSMutableArray in TableView Which contains set of Views 如何在Swift 3中删除包含NSDictionary的NSMutableArray中的重复字段 - How to remove duplicated fields in NSMutableArray which includes NSDictionary in Swift 3 检查NSDictionary的NSMutableArray中是否存在对象 - Check if an object exist in a NSMutableArray of NSDictionary 如何将NSDictionary转换为包含NSDictionary的json的NSString? - How to convert NSDictionary to NSString which contains json of NSDictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM