NSArray:
indexOfObjectIdenticalTo:针对的对象地址,当对象是字符串或者数值时,不能根据地址找出两个重复中的其中一个;当对象是OC对象时,可以。(估计是因为字符串和数值存放在栈:相同的两个内存中的存储地址一样,所以不能利用地址区分。OC对象在堆)。字符串的mutableCopy也在堆区,所以mutable后是两个储存地址,也可区分;浅拷贝的copy就不行了。eg.
NSObject *str1 = @"3";
NSObject *str2 = [@"3" mutableCopy];
NSObject *str3 = @"5";
NSObject *sss = [[UIButton alloc] init];
NSInteger one = 1;
NSInteger two = 1;
NSInteger three = 3;
NSMutableArray *mA = [NSMutableArray arrayWithObjects:str1,str2,str3, nil];
NSInteger ii = [mA indexOfObjectIdenticalTo:str2];componentsJoinedByString: 数组变字符串,并在元素中插入string
NSArray *arr = @[@"12",@"34",@"5",@"6"];
NSString *str = [arr componentsJoinedByString:@"wgj"];
NSLog(@"%@",str);//结果:@"12wgj34wgj5wgj6"。descriptionWithLocale:等方法,打印,可以自定义,但不要打印self,会循环。后面的参数是用来优化打印的信息,可以在打印的时候,利用参数控制打印的一些自定义格式。
firstObjectCommonWithArray:
NSArray *arr2 = @[@"34",@"mi",@"12"];
NSArray *arr = @[@"12",@"34",@"5",@"6"];
//对比相同的元素,前后对比的对象顺序写法有关
NSString *str2 = [arr2 firstObjectCommonWithArray:arr];//结果@"34"
NSString *str = [arr firstObjectCommonWithArray:arr2];//结果@"12-
sortedArrayUsingFunction:把数组转成data,利用函数的规则排序
NSArray *arr = @[@"322",@"1",@"22"];
NSData *data = [arr sortedArrayHint];
NSArray *reArr = [arr sortedArrayUsingFunction:sortType context:@"wg" hint:data];
NSLog(@"%@",reArr);
sortType: 自定义规则的函数
NSInteger sortType(id st,id str,void *cha)
{
NSString *s1 = (NSString *)st;
NSString *s2 = (NSString *)str;if(s1.length > s2.length)
{
return NSOrderedAscending;
}else if(s1.length < s2.length)
{
return NSOrderedDescending;
}return NSOrderedSame;
} -
sortedArrayUsingSelector: 自定义一个类对象,类里面声明和实现selector的方法,方法中自定义比较的规则;数组中盛放自定义的类对象,然后会生成比较后的数组结果。eg.
Man *man1 = [Man sortAllocWithN:@"a" num:19];
Man *man2 = [Man sortAllocWithN:@"c" num:14];
Man *man3 = [Man sortAllocWithN:@"b" num:13];NSArray *arr = @[man1,man2,man3]; NSArray *array2 = [arr sortedArrayUsingSelector:@selector(myCompare2:)]; for (Man *sort in array2) { NSLog(@"%@", sort.name); }
其中,Man:.h:
@interface Man : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger number;
- (NSComparisonResult)myCompare:(NSString *)name;
- (NSComparisonResult)myCompare2:(Man *)sort;
- (Man *)sortAllocWithN:(NSString *)name num:(NSInteger)num;
@end
.m:
- (Man *)sortAllocWithN:(NSString *)name num:(NSInteger)num {
Man * sort1 = [[Man alloc] init];
sort1.name = name;
sort1.number = num;
return sort1;
}
-
(NSComparisonResult)myCompare:(Man *)sort {
return [self.name compare:sort.name];
}
// 该方法用来排序数字
-
(NSComparisonResult)myCompare2:(Man *)sort {
if ( self.number > sort.number ) {
return NSOrderedDescending;
} else if (self.number < sort.number ) {
return NSOrderedAscending;
} else {
return NSOrderedSame;
}
}
subarrayWithRange: 根据range生成新的子数组。
- (BOOL)writeToFile: 和 - (BOOL)writeToURL: 写入存储,写入文件
NSString *cachesDirectoryPath = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"test.txt"];
NSURL *url2 = [NSURL fileURLWithPath:cachesDirectoryPath];
NSString *reStr = [NSString stringWithContentsOfURL:url2 encoding:NSUTF8StringEncoding error:nil];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[reStr dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];//data:也可以:NSData *data = [NSData dataWithContentsOfFile:path];
- (BOOL)writeToFile: 和 - (BOOL)writeToURL: 写入存储,写入文件
makeObjectsPerformSelector和makeObjectsPerformSelector:(SEL)aSelector withObject:。这两个方法让数组的每个对象执行事件,不同在于对象中的方法实现:一个可以不带参数,一个可以带参数。e.g.
Man *man1 = [Man sortAllocWithN:@"wg01" num:1];
Man *man2 = [Man sortAllocWithN:@"wg02" num:2];
NSArray *arr = @[man1 ,man2];
[arr makeObjectsPerformSelector:@selector(test01)];objectsAtIndexes: 根据indexs的集合获取新的数组。objectAtIndexedSubscript:根据下标获取单个元素。
10.enumerateObjectsUsingBlock:等,是通过枚举的方式,遍历数组。
-
indexOfObjectPassingTest: 进行判断,筛选操作,得出想要的元素;
indexesOfObjectsPassingTest:得出想要的元素数组e.g..
NSArray *array = @[@"12",@"mi",@"34",@"m",@"34"];
NSIndexSet *ld = [array indexesOfObjectsPassingTest:^BOOL(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isEqualToString:@"34"]) {
NSLog(@"%@",obj);
return YES;
}else{
NSLog(@"NO:%@",obj);
return NO;
}}];
-
sortedArrayUsingComparator: block里面进行排序,得到新数组eg.
NSArray *array = @[@"12",@"66",@"34",@"1",@"34"];
NSArray *resA = [array sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
if ([obj1 compare:obj2] == NSOrderedAscending) {
return NSOrderedAscending;
}else if ([obj1 compare:obj2] == NSOrderedDescending){
return NSOrderedDescending;
}else{
return NSOrderedSame;
}}]; NSLog(@"%@",resA);
- (NSUInteger)indexOfObject:(ObjectType)obj inSortedRange:(NSRange)r options:(NSBinarySearchingOptions)opts usingComparator:(NSComparator NS_NOESCAPE)cmp,是排序后的查找某元素,利用的折叠法查找,需要先排序
- (id)array;创建一个空数组
- (id)arrayWithObject:(id)anObject;指定一个元素创建数组对象
- (id)arrayWithObjects:(const id [])objects count:(NSUInteger)cnt;指定个数从一个数组对象创建新的数组对象(类消息)
- (id)arrayWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;指定多个元素创建数组对象
- (id)arrayWithArray:(NSArray *)array;生成新另外一个数组
- (id)initWithObjects:(const id [])objects count:(NSUInteger)cnt;指定个数从一个数组对象创建新的数组对象(实例消息)
- (id)initWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION; 指定多个元素创建数组对象
- (id)initWithArray:(NSArray *)array;从已有数组初始化
- (id)initWithArray:(NSArray *)array copyItems:(BOOL)flag;从已有数组初始化(标示作用,浅复制,还是深复制)
- (id)arrayWithContentsOfFile:(NSString *)path;从文件加载数据
- (id)arrayWithContentsOfURL:(NSURL *)url;从网络地址加载数据
- (id)initWithContentsOfFile:(NSString *)path;从文件加载数据
- (id)initWithContentsOfURL:(NSURL *)url;从网络地址加载数据
其他:利用kvc对数组进行一些操作:
获取数组里的,最大、最小、平均、求和
NSArray *array = @[@"1",@"3",@2,@9.5,@"1.2"];
NSNumber *sum = [array valueForKeyPath:@"@sum.floatValue"];
NSNumber *avg = [array valueForKeyPath:@"@avg.floatValue"];
NSNumber *max = [array valueForKeyPath:@"@max.floatValue"];
NSNumber *min = [array valueForKeyPath:@"@min.floatValue"];
NSLog(@"sum:%@",sum);
NSLog(@"avg:%@",avg);
NSLog(@"max:%@",max);
NSLog(@"min:%@",min);
删除重复数据
NSArray *array = @[@"name", @"w", @"aa", @"zxp", @"aa"]; //返回的是一个新的数组
NSArray *newArray = [array valueForKeyPath:@"@distinctUnionOfObjects.self"];
NSLog(@"%@", newArray);
同样可以嵌套使用,先剔除name对应值的重复数据再取值
NSArray array = @[ @{@"title":@"zxp",@"name":@"zhangxiaoping"}, @{@"title":@"zxp2",@"name":@"zhangxiaoping2"}, @{@"title":@"zxp",@"name":@"zhangxiaoping3"}, @{@"title":@"zxp",@"name":@"zhangxiaoping"}];
//根据name字段,来进行重复删除。
NSArray newArray = [array valueForKeyPath:@"@distinctUnionOfObjects.name"];
//如果要根据title字段来删除重名的写法为@distinctUnionOfObjects.title
NSLog(@"%@", newArray);
//可以从对象组成的数组中,获取数组中每个对象中字段price的和:用
@UnionOfObjects.price
//相同的价格也相加
/
print:( zhangxiaoping3, zhangxiaoping2, zhangxiaoping)是一个字符串数组/
@distinctUnionOfObjects //清除重复值
@unionOfObjects //保留重复值
进行实例方法的调用
NSArray *array = @[@"name", @"w", @"aa", @"ZXPing"];
NSLog(@"%@", [array valueForKeyPath:@"uppercaseString"]);
相当于数组中的每个成员执行了uppercaseString方法,然后把返回的对象组成一个新数组返回。既然可以用uppercaseString方法,那么NSString的其他方法也可以,比如[array valueForKeyPath:@"length"]。当然,其他对象的实例方法也可以以此类推来进行调用~!