使用NSPredicate过滤包含自定义类对象的多级数组

时间:2016-09-10 07:43:08

标签: ios objective-c nsarray nspredicate

我有一个NSMutalbeArray,其中包含另外两个数组,其中一个包含字符串值,另一个包含自定义对象的NSMutableArray

自定义类

@interface Message : NSObject<NSCoding>

@property (nonatomic,strong) NSString *strMessgaeID;
@property (nonatomic,strong) NSString *strUserID;
@property (nonatomic,strong) NSString *strToUserID;
@property (nonatomic,strong) NSString *strMessgae;
@property (nonatomic,strong) NSString *strMessageDateTime;

@end

要在数组中添加对象,我使用了这种类型的代码

 NSMutableArray  *arrMessages = [NSMutableArray alloc]init];
 NSPredicate *predicate       = [NSPredicate predicateWithFormat:@"strMessageDateTime contains[c] %@",strConvertedDate];
 NSMutableArray *arrResult    = [[arrSortedMessages filteredArrayUsingPredicate:predicate] mutableCopy];
 arrMessage                   = [@[strDate,arrResult] mutableCopy];
 [arrMessages addObject:arrMessage];

现在更新一个对象并刷新我的UITabeList

    NSString *MSGID = @"4"
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"strMessgaeID == %@", MSGID];
// I have also try following 
//     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF[1].strMessgaeID == %@", MSGID];
//     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SELF[1].strMessgaeID == %@", MSGID];

    Message *objMessage    = [[arrMessages filteredArrayUsingPredicate:predicate] firstObject];

    if (objMessage != nil) {
         [arrMessages replaceObjectAtIndex:index withObject:objMessage];
         [tblMessages reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
    }

但是对于上述任何方式,我都无法让自定义对象更新其值。

0 个答案:

没有答案