obj -c-将nsdictionary添加到nsmutablearray



我正在尝试在使用以下代码的现有NSMutableArray中添加包含一系列键的Nsdictionary。但是,运行该应用程序后,我在最后一行( [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.messages filledArayusingPredicate:pfinal];

最新更新