react-native-navigation的迁移库

NSArray+utils.m 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #import "NSArray+utils.h"
  2. #import <UIKit/UIKit.h>
  3. @implementation NSArray (utils)
  4. - (NSArray *)intersect:(NSArray *)array withPropertyName:(NSString *)propertyName {
  5. NSMutableArray* intersection = [NSMutableArray new];
  6. for (NSObject* object in array) {
  7. NSArray* filteredArray = [self filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"%@ == %@", propertyName, object]];
  8. [intersection addObjectsFromArray:filteredArray];
  9. }
  10. return [NSArray arrayWithArray:intersection];
  11. }
  12. - (NSArray *)difference:(NSArray *)array withPropertyName:(NSString *)propertyName {
  13. NSMutableArray* diff = [NSMutableArray arrayWithArray:self];
  14. NSArray* intersection = [self intersect:array withPropertyName:propertyName];
  15. [diff removeObjectsInArray:intersection];
  16. return [NSArray arrayWithArray:diff];
  17. }
  18. - (NSArray *)mapObjectsUsingBlock:(id (^)(id obj, NSUInteger idx))block {
  19. NSMutableArray *result = [NSMutableArray arrayWithCapacity:[self count]];
  20. [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  21. [result addObject:block(obj, idx)];
  22. }];
  23. return result;
  24. }
  25. - (NSArray *)sortByPropertyName:(NSString *)propertyName {
  26. return [self sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
  27. id first = [a valueForKey:propertyName];
  28. id second = [b valueForKey:propertyName];
  29. return [first compare:second];
  30. }];
  31. }
  32. @end