Fastest way to check if an array contains the same objects of another array

后端 未结 10 1922
夕颜
夕颜 2020-12-29 04:36

The goal is to compare two arrays as and check if they contain the same objects (as fast as possible - there are lots of objects in the arrays). The arrays cannot be checked

10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-29 05:07

    NSArray *filtered = [someArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"someParamter == %@", paramValue]]];
    if (filtered.count) {
    
    }
    

    the main plus is you can use it for any kind of objects: custom, system, NSDictionary. for example I need to know is my UINavigationController's stack contains MySearchResultsVC and MyTopMenuItemsVC or not:

        NSArray *filtered = [self.navigationController.viewControllers filteredArrayUsingPredicate:
                                         [NSPredicate predicateWithFormat:@"class IN %@",
                                          [NSArray arrayWithObjects:
                                           [MySearchResultsVC class],
                                           [MyTopMenuItemsVC class],
                                           nil]]];
    if (filtered) {
    /* ok, now we can handle it! */
    }
    

提交回复
热议问题