iPhone SDK提供了多种方法来删除NSArray中的重复项。以下是一种常见的方法:
示例代码:
NSArray *arrayWithDuplicates = @[@"A", @"B", @"C", @"A", @"B"];
NSSet *setWithoutDuplicates = [NSSet setWithArray:arrayWithDuplicates];
NSArray *arrayWithoutDuplicates = [setWithoutDuplicates allObjects];
示例代码:
NSArray *arrayWithDuplicates = @[@"A", @"B", @"C", @"A", @"B"];
NSOrderedSet *orderedSetWithoutDuplicates = [NSOrderedSet orderedSetWithArray:arrayWithDuplicates];
NSArray *arrayWithoutDuplicates = [orderedSetWithoutDuplicates array];
示例代码:
NSArray *arrayWithDuplicates = @[@"A", @"B", @"C", @"A", @"B"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", arrayWithDuplicates];
NSArray *arrayWithoutDuplicates = [arrayWithDuplicates filteredArrayUsingPredicate:predicate];
以上是三种常见的方法来删除NSArray中的重复项。根据具体的需求和场景,选择合适的方法进行去重操作。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云