1、查找:
数组中某个元素时候存在:
NSArray *array =@[@"111",@"abc",@"sss",@"222",@"111"];
if ([array containsObject:@"abc"]) {
NSLog(@"[array containsObject:abc]");
}
数组中某个元素的存储位置:
SString *str = @"111";
NSUInteger itemIndex = [array indexOfObject:str];
if (itemIndex == NSNotFound) {
NSLog(@"%@ index is NSNotFound",str);
} else {
NSLog(@"%@ index is %lu",str,itemIndex);
}
2、遍历:
NSArray *array =@[@"111",@"222",@"333"];
//常规方式:
for (int i = 0; i < array.count; i++) {
NSLog(@"for:%@",array[i]);
}
for (NSString *item in array) {
NSLog(@"forin:%@",item);
}
特殊方法:
[array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"index:%lu,object:%@",idx,obj);
}];
[array enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"index:%lu,object:%@",idx,obj);
}];
/*
typedef NS_OPTIONS(NSUInteger, NSEnumerationOptions) {
NSEnumerationConcurrent = (1UL << 0),//并发
NSEnumerationReverse = (1UL << 1),//逆序
};
*/
3、增、删、改:
NSMutableArray *array = [[NSMutableArray alloc] init];
//数组添加
[array addObject:@"111"];//@[@"111"]
[array insertObject:@"ddd" atIndex:0];//@[@"ddd",@"111"]
[array addObjectsFromArray:@[@"222",@"bbb"]];//@[@"ddd",@"111",@"222",@"bbb"]
//数组修改
[array exchangeObjectAtIndex:0 withObjectAtIndex:1];//@[@"111",@"ddd",@"222",@"bbb"]
[array replaceObjectAtIndex:2 withObject:@"ccc"];//@[@"111",@"ddd",@"ccc",@"bbb"]
//数组删除
[array removeObject:@"ddd"];//@[@"111",@"ccc",@"bbb"]
[array removeLastObject];//@[@"111",@"ccc"]
[array removeObjectAtIndex:0];//@[@"ccc"]
[array removeObjectsInArray:@[@"ccc"]];//@[]
[array removeAllObjects];//@[]