我正在尝试使用以下代码将包含一系列键的NSDictionary添加到现有NSMutableArray。但是,在运行该应用程序时,我在最后一行([self.filtered addObject:nodeData];)上看到崩溃:“ EXC_BAD_ACCESS”,我不确定为什么吗?
ViewController.h
@property (nonatomic, strong) NSMutableArray *filtered;
ViewController.m
-(void)viewDidLoad {
NSString *targetedUser = [self.messageDataFriends objectForKey:@"uid"];
NSString *universal = @"9506";
NSString *myID = [session user][@"user"][@"uid"];
NSPredicate *p1 = [NSPredicate predicateWithFormat:@"uid ==[cd] %@", targetedUser];
NSPredicate *p2 = [NSPredicate predicateWithFormat:@"targetuser ==[cd] %@", myID];
NSPredicate *p3 = [NSPredicate predicateWithFormat:@"uid ==[cd] %@", myID];
NSPredicate *p4 = [NSPredicate predicateWithFormat:@"targetuser ==[cd] %@", targetedUser];
NSPredicate *p5 = [NSPredicate predicateWithFormat:@"targetuser ==[cd] %@", universal];
NSCompoundPredicate *pIntermediary1 = [NSCompoundPredicate andPredicateWithSubpredicates:@[p1, p2]];
NSCompoundPredicate * pIntermediary2 = [NSCompoundPredicate andPredicateWithSubpredicates:@[p3, p4]];
NSCompoundPredicate * pIntermediary3 = [NSCompoundPredicate andPredicateWithSubpredicates:@[p1, p5]];
NSCompoundPredicate *pFinal = [NSCompoundPredicate orPredicateWithSubpredicates:@[pIntermediary1, pIntermediary2, pIntermediary3]];
_filtered = [self.messages filteredArrayUsingPredicate:pFinal];
}
- (IBAction)sendReply:(id)sender {
NSMutableDictionary *nodeData = [NSMutableDictionary new];
[nodeData setObject:@"messages" forKey:@"type"];
NSDictionary *messageValues = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:sentText, nil] forKeys:[NSArray arrayWithObjects:@"value", nil]];
NSDictionary *finalMessage = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:messageValues] forKey:@"und"];
[nodeData setObject:finalMessage forKey:@"body"];
self.replyField.text = @"";
NSString *targetedUser = [self.messageData objectForKey:@"uid"];
NSDictionary *targetMessage = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:targetedUser, nil] forKeys:[NSArray arrayWithObjects:@"value", nil]];
NSDictionary *finalUser = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:targetMessage] forKey:@"und"];
[nodeData setObject:finalUser forKey:@"field_targetuser"];
[nodeData setValue: @"Re:" forKey:@"title"];
[self.filtered addObject:nodeData];
NSLog(@"SHOW UPDATED FILTERED %@", self.filtered);
}
最佳答案
我认为这条线
_filtered = [self.messages filteredArrayUsingPredicate:pFinal];
是问题。您正在直接访问ivar,避免调用属性设置器。这不会增加保留计数。
替换为self.filtered = [self.messagesfilteredArrayUsingPredicate:pFinal];