NSArray *addingAry = [NSArray array];
NSArray *myAry = @[@"a",@"ちゃん",@"李",@"田",@"孔"];
//- (NSArray<ObjectType> *)arrayByAddingObject:(ObjectType)anObject;
//不可变数组添加单个元素
addingAry = [myAry arrayByAddingObject:@"孙"];
//- (NSArray<ObjectType> *)arrayByAddingObjectsFromArray:(NSArray<ObjectType> *)otherArray;
//把另一数组元素拼接过来
addingAry = [myAry arrayByAddingObjectsFromArray:@[@"赵",@"关",@"钱"]];
NSLog(@"%@",addingAry);
//- (NSString *)componentsJoinedByString:(NSString *)separator;
//把元素根据指定字符分隔成 String
NSString *componentStr = [myAry componentsJoinedByString:@"-"];
//- (BOOL)containsObject:(ObjectType)anObject;
//是否包含某个元素
BOOL isContains = [addingAry containsObject:@"陈"];
//@property (readonly, copy) NSString *description;
//元素当前编码格式的字符串
NSString *descriptionStr = addingAry.description;
//- (NSString *)descriptionWithLocale:(nullable id)locale;
//TODO:本地化字符串
//目前参考相关资料,NSArray 和 NSDictionary 分类重写时候用,这样在打印的时候本地化显示元素内容
//重写方法在 分类 “NSArray+ArrayLocal.h”
NSLocale *locale = [NSLocale currentLocale];
// NSString *localeDescriptionStr = [addingAry descriptionWithLocale:locale];
/*未重写前
a,
"\U5f20",
"\U674e",
"\U7530",
"\U5b54",
"\U8d75",
"\U5173",
"\U94b1"
*/
/*重写后 打印数组
a,
ちゃん,
李,
田,
孔,
赵,
关,
钱,
*/
//- (NSString *)descriptionWithLocale:(nullable id)locale indent:(NSUInteger)level;
//本地化后,缩进值 (无语,只是本地化同时允许打印缩进), 分类没重写带缩进方法,只是测试缩进效果
NSString *localeAndLevDescriptionStr = [addingAry descriptionWithLocale:locale indent:3];
/* indent 0
(
a,
"\U3061\U3083\U3093",
"\U674e",
"\U7530",
"\U5b54",
"\U8d75",
"\U5173",
"\U94b1"
)
*/
/* indent 3
(
a,
"\U3061\U3083\U3093",
"\U674e",
"\U7530",
"\U5b54",
"\U8d75",
"\U5173",
"\U94b1"
)
*/
//- (nullable ObjectType)firstObjectCommonWithArray:(NSArray<ObjectType> *)otherArray;
//addingAry 首元素 与 另一数组的元素交集,如果有相同于首元素返回该元素值,如果不相同返回nil
id commonObject = [addingAry firstObjectCommonWithArray:@[@"absdf",@"a"]];
//- (void)getObjects:(ObjectType __unsafe_unretained [])objects range:(NSRange)range;
//从range 范围获取元素赋值给 objects
检索Block方法、高效枚举方法
//- (NSUInteger)indexOfObjectIdenticalTo:(ObjectType)anObject;
//- (NSUInteger)indexOfObject:(ObjectType)anObject;
//返回相同值的最低索引
NSUInteger i = [addingAry indexOfObjectIdenticalTo:@"田"];//3
NSUInteger ii = [addingAry indexOfObject:@"田"];//3
//- (NSEnumerator<ObjectType> *)objectEnumerator;
//获取正序排列集合,通过 nextObject函数删除0下标元素
NSArray *myAry1 = @[@"a",@"b",@"d",@"ff",@"c"];
NSEnumerator *enumerator = [myAry1 objectEnumerator];
id obj;
while( obj=[enumerator nextObject])
{
//遍历完后,数组已经清空
NSLog(@"%@",obj);
/*
a,
b,
d,
ff,
c
*/
}
//执行一次删除下标0元素
id enumObject = [enumerator nextObject];
[enumerator nextObject];
//返回数组
NSArray *allAry = enumerator.allObjects;
//- (NSEnumerator<ObjectType> *)reverseObjectEnumerator;
//获取反序排列集合
enumerator = [myAry1 reverseObjectEnumerator];
allAry = enumerator.allObjects;
/*
c,
ff,
d,
b,
a
*/
//自定义排序,混合使用比较复杂
//@property (readonly, copy) NSData *sortedArrayHint;
//- (NSArray<ObjectType> *)sortedArrayUsingFunction:(NSInteger (*)(ObjectType, ObjectType, void * __nullable))comparator context:(nullable void *)context;
//- (NSArray<ObjectType> *)sortedArrayUsingFunction:(NSInteger (*)(ObjectType, ObjectType, void * __nullable))comparator context:(nullable void *)context hint:(nullable NSData *)hint;
NSData *data = [allAry sortedArrayHint];
NSArray *a = [allAry sortedArrayUsingFunction:sortType context:nil hint:data];
//- (NSArray<ObjectType> *)subarrayWithRange:(NSRange)range;
//截取range范围元素,返回一个新数组
NSRange subRange = {1,3};
NSArray *subAry = [allAry subarrayWithRange:subRange];
//- (void)makeObjectsPerformSelector:(SEL)aSelector
//- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(nullable id)argument
//数组每一个元素执行一次 SEL ,元素只能是实例对象
//*********************************
//示例1 更改所有button selected 属性
UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
btn1.selected = NO;
UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(0, 200, 100, 100)];
btn1.selected = NO;
NSMutableArray *btnAry = [NSMutableArray arrayWithObjects:btn1,btn2,nil];
[btnAry makeObjectsPerformSelector:@selector(setSelected:) withObject:@YES];
//示例2 代替 for in 清空View
[[self.view subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
//示例3 调用method
__weak TRViewController *VC = self;
SEL print = @selector(printfSomeTH);
NSArray *objectAry = @[VC];
[objectAry makeObjectsPerformSelector:print];
//********************************
//- (void)enumerateObjectsUsingBlock:(void (^)(ObjectType obj, NSUInteger idx, BOOL *stop))block
//遍历数组,block返回每个数组元素、下标
[allAry enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
;
NSLog(@"block===obj:%@ idx:%ld stop:%s",obj,(unsigned long)idx,stop);
if ([obj isEqualToString:@"b"])
{
*stop = YES; //停止循环
}
}];
//- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(ObjectType obj, NSUInteger idx, BOOL *stop))block
//NSEnumerationConcurrent(正向遍历,同时回调两次打印) NSEnumerationReverse(反向遍历,一次一次打印)
__block NSUInteger num = 0;
[allAry enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
num++;
NSLog(@"Options===obj:%@ idx:%ld stop:%ud",obj,(unsigned long)idx,num);
}];
//- (void)enumerateObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts usingBlock:(void (^)(ObjectType obj, NSUInteger idx, BOOL *stop))block
//指定范围的数组元素遍历
NSRange indexRange = {1,3};
NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:indexRange];
[allAry enumerateObjectsAtIndexes:set options:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"AtIndexes===obj:%@ idx:%ld stop:%s",obj,(unsigned long)idx,stop);
}];
//- (NSUInteger)indexOfObjectPassingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate
//循环判断是否满足指定条件,满足返回 元素下标 结束遍历,不满足一直遍历完数组返回垃圾值
NSUInteger passingTestNum = [allAry indexOfObjectPassingTest:^BOOL(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isEqualToString:@"ff"])
{
return YES;
}
return NO;
}];
if (passingTestNum==1)
{
;
}
//- (NSUInteger)indexOfObjectWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate
//正反向遍历
//- (NSUInteger)indexOfObjectAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate
//指定范围+正反向遍历
//- (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate
//指定范围遍历数组元素,如果满足条件返回当前元素下标,
NSIndexSet *indexSet = [allAry indexesOfObjectsAtIndexes:set options:NSEnumerationConcurrent passingTest:^BOOL(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isEqualToString:@"d"])
{
return YES;
}
return NO;
}];
NSUInteger fir = indexSet.firstIndex;