removeObjectIdenticalTo函数会移除数组中所有地址相同的元素
//
// main.m
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSNumber *num_1 = @1;
NSNumber *num_2 = @2;
NSNumber *num_3 = @3;
NSNumber *num_4 = @1;
NSNumber *num_5 = @5;
NSMutableArray *nums = [NSMutableArray arrayWithObjects:num_1, num_2, num_3, num_4, num_5, nil];
NSLog(@"1 --> before remove:");
for (NSNumber *number in nums) {
NSLog(@"%p %@", number, number);
/**
1 --> before remove:
0x187 1
0x287 2
0x387 3
0x187 1
0x587 5
*/
}
[nums removeObjectIdenticalTo:num_1];
NSLog(@"2 --> after remove:");
for (NSNumber *number in nums) {
NSLog(@"%p %@", number, number);
/**
2 --> after remove:
0x287 2
0x387 3
0x587 5
*/
}
NSString *str_1 = @"hello";
NSString *str_2 = [[NSString alloc] initWithString:@"hello"];
NSString *str_3 = @"hello";
NSString *str_4 = [NSString stringWithFormat:@"%@", @"hello"];
NSString *str_5 = [[NSString alloc] initWithFormat:@"%@", str_1];
NSString *str_6 = [NSString stringWithString:str_4];
NSString *str_7 = @"hello_2";
NSMutableArray *strArray = [NSMutableArray arrayWithObjects:str_1, str_2, str_3, str_4, str_5, str_6, str_7, nil];
NSLog(@"3 --> before remove:");
for (NSString *string in strArray) {
NSLog(@"%p %@", string, string);
/**
3 --> before remove:
0x100002128 hello
0x100002128 hello
0x100002128 hello
0x102300520 hello
0x1023004f0 hello
0x102300520 hello
0x100002168 hello_2
*/
}
[strArray removeObjectIdenticalTo:str_4];
NSLog(@"4 --> after remove:");
for (NSString *string in strArray) {
NSLog(@"%p %@", string, string);
/**
4 --> after remove:
0x100002128 hello
0x100002128 hello
0x100002128 hello
0x1023004f0 hello
0x100002168 hello_2
*/
}
[strArray removeObject:str_1];
NSLog(@"5 -->");
for (NSString *string in strArray) {
NSLog(@"%p %@", string, string);
/**
5 -->
0x100002168 hello_2
*/
}
}
return 0;
}