iOS 用簡便的方法統計兩個陣列中不同的元素

weixin_34253539發表於2019-02-20

在這裡用的是正則匹配

 NSArray *arrayBase = @[@1, @2, @3, @4, @5, @6];
    NSArray *arrayCom = @[@1, @4, @7, @11, @8, @"hh"]; 
    NSArray *data1Array = [arrayBase filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"NOT (SELF IN %@)",arrayCom]];//篩選arrayBase中不含有arrayCom的
    NSArray *data2Array = [arrayCom filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"NOT (SELF IN %@)",arrayBase]];//篩選arrayCom中不含有arrayBase的
    NSMutableArray *totolArray = [NSMutableArray new];
    [totolArray addObjectsFromArray:data1Array];
    [totolArray addObjectsFromArray:data2Array];
    NSLog(@"不同的元素 %@",totolArray);

相關文章