#import <Foundation/Foundation.h>
//排序算法,应用于 NSArray *arr=[arrs1 sortedArrayUsingFunction:sortByID context:nil];排序
NSInteger sortByID(id obj1, id obj2, void *context){
NSString *str1 =(NSString*) obj1;
NSString *str2 =(NSString *) obj2;
if (str1.length < str2.length) {
return NSOrderedDescending;
}
else if(str1.length == str2.length)
{
return NSOrderedSame;
}
return NSOrderedAscending;
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
//数组只能存储oc对象,NSArray一旦创建不能修改
NSArray *arrs0=@[@"123",@"456",@"789"];
//初始化数组的一种奇葩
NSArray *arrs1=[NSArray arrayWithArray:arrs0];
//1.获取数组中的元素对象
id need=[arrs1 objectAtIndex:1];//取出索引位置中的对象
id nn=[arrs1 objectAtIndexedSubscript:1];
NSLog(@"need=%@",need);
//2.元素个数
int t=[arrs1 count];
//3.判断是否包含
bool isYes=[arrs1 containsObject:@"123"];
//4.取得数组中第一个和最后一个元素
need= [arrs1 firstObject];
need=[arrs1 lastObject];
//5.添加元素,返回另一个数组
arrs0=[arrs1 arrayByAddingObject:@"000"];
//6.合并两个数组
arrs0=[arrs1 arrayByAddingObjectsFromArray:arrs1];
NSLog(@"%@",arrs0);
//7.取得数组一定范围内的数据
arrs0=[arrs1 subarrayWithRange:NSMakeRange(1, 1)];
NSLog(@"%ld",[arrs0 count]);
//8.判断是否相等
isYes=[arrs0 isEqualToArray:arrs1];
//9.返回元素的位置
int tt=[arrs0 indexOfObject:@"123"];
NSLog(@"%d",tt);
tt=[arrs0 indexOfObjectIdenticalTo:@"123"];
//10.数组与字符串的转换
NSString *sss=[arrs1 componentsJoinedByString:@"-"];
//11.字符串与数组转换
NSArray *array=[sss componentsSeparatedByString:@"-"];
NSLog(@"%@",sss);
NSLog(@"%@",array);
//sortedArrayHint:不知道怎么用的
//int num=[array sortedArrayHint];
//NSLog(@"%ld",num);
//12.写入文件,写入url
[arrs1 writeToFile:@"/users/tang/desktop/array.plist" atomically:YES];
NSArray *arrfile=[NSArray arrayWithContentsOfFile:@"/users/tang/desktop/array.plist"];
NSLog(@"%@",arrfile);
//13.数组的遍历 还有两种
[arrs1 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%d,%@",idx,obj);
if(idx==1)
{
*stop=YES;
}
}];
//14.正向遍历
NSEnumerator *dfd=[arrs1 objectEnumerator];
for (id dd in dfd) {
NSLog(@"%@",dd);
}
//15.反向遍历
dfd=[arrs1 reverseObjectEnumerator];
for (id dd in dfd) {
NSLog(@"%@",dd);
}
//16用自己写的函数实现数组的排序
NSArray *arr=[arrs1 sortedArrayUsingFunction:sortByID context:nil];
// NSLog(@"%ld",NSNotFound);
}
return 0;
}