#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
//数组用来存储对象
//创建 NSNumber 对象,作为 NSArray添加对象
NSNumber *Num1=[[NSNumber alloc] initWithInt:1];
NSNumber *Num2=[[NSNumber alloc] initWithInt:2];
NSNumber *Num3=[[NSNumber alloc] initWithInt:3];
//1.创建数组 NSArray
NSArray *Arr1=[NSArray arrayWithObjects:@"one", @"two",@"three",@"four",@"five",nil];
NSLog(@"%@",Arr1);
//输出
/*
(
one,
two,
three,
four,
five
)
*/
NSArray *Arr2=[[NSArray alloc ] initWithObjects:Num1,Num2,Num3,@"two", nil];
NSLog(@"%@",Arr2);
//输出
/*
(
1,
2,
3
)
*/
//2.获取数组中指定元素
NSLog(@"%@", [Arr1 objectAtIndex:1]);//输出 two
NSLog(@"%@",[Arr1 objectAtIndexedSubscript:2]);//以下标索引 输出 three
//3.获取元素个数
NSLog(@"%li",[Arr1 count]);//输出 5
//4.判断object是否在数组中 返回BOOL类型 示例
BOOL Bool1=[Arr1 containsObject:@"one"];
NSLog(@"%i",Bool1);
//5.将数组元素以“YY”分割合并 实现从数组到字符串
NSString *Str=[Arr1 componentsJoinedByString:@"YY"];
NSLog(@"%@",Str);//输出 oneYYtwoYYthreeYYfourYYfive
//实现从字符串到数组
NSString *String=@"one,two,three,four";
NSArray *Arra=[String componentsSeparatedByString:@","];
NSLog(@"%@",Arra);
//输出
/*
(
one,
two,
three,
four
)
*/
//6.判断两个数组第一个相同的元素
NSString *Str2=[Arr1 firstObjectCommonWithArray:Arr2];
NSLog(@"%@",Str2); //输出 two
//7.从一个数组得到元素
NSArray *Arr3=[NSArray arrayWithArray:Arr1];
NSLog(@"%@",Arr3);
//输出
/*
(
one,
two,
three,
four,
five
)
*/
//8.从 const id [] 获取 object 该方法要注意使用 易错
NSString *pStr[5];
pStr[0]=@"ff";
pStr[1]=@"one";
pStr[2]=@"two";
NSArray *Arr4=[NSArray arrayWithObjects:pStr count:3];
NSLog(@"%@",Arr4);
//输出
/*
(
ff,
one,
two
)
*/
//9.在一定范围查找object位置
NSUInteger nNum=[Arr1 indexOfObject:@"three" inRange:NSMakeRange(1, 3)];
NSLog(@"%lu",(unsigned long)nNum);//2
//10创建可变动态性数组 NSMutableArray
NSMutableArray *MuArr=[NSMutableArray arrayWithCapacity:20];
MuArr=[NSMutableArray arrayWithArray:Arr1];
//11.快速枚举输出
for (id obj in MuArr) {
NSLog(@"枚举输出 %@",obj);//会一个一个输出
}
//12.NSMutableArray 是 NSArray 子类 有多出一些方法 如 添加 替换 插入等
//替换
[MuArr replaceObjectAtIndex:1 withObject:@"off"];
NSLog(@"%@",MuArr);
//输出
/*
(
one,
off,
three,
four,
five
)
*/
//13 .移除部分 移除全部等
[MuArr removeAllObjects];
NSLog(@"%li",[MuArr count]);//0
//14.添加对象
[MuArr addObject:@"one"];
[MuArr addObject:@"two"];
NSLog(@"%@",MuArr);
//15.将结构体添加至数组中 (NSRange NSSize NSRect NSPoint等)
NSRange range=NSMakeRange(3, 4);
[MuArr addObject:[NSValue valueWithRange:range]];
NSRect rect=NSMakeRect(1, 1, 10, 20);
[MuArr addObject:[NSValue valueWithRect:rect]];
NSLog(@"%@",MuArr);
//输出
/*
(
one,
two,
"NSRange: {3, 4}",
"NSRect: {{1, 1}, {10, 20}}"
)
*/
//16.用枚举器 NSEnumerator 浏览
NSEnumerator *Enumerator1=[MuArr objectEnumerator];
id object1=nil;
while (object1=[Enumerator1 nextObject]) {
NSLog(@"%@",object1);
}
//17.用枚举器 NSEnumerator 从后向前浏览、
//创建 NSEnumerator
NSEnumerator *Enumerator2=[MuArr reverseObjectEnumerator];
id object2=nil;
while (object2=[Enumerator2 nextObject]) {
NSLog(@"%@",object2);
}
//输出
/*
NSRect: {{1, 1}, {10, 20}}
NSRange: {3, 4}
two
one
*/
}
return 0;
}
Objective-C NSArray 基础 (NSString NSNumber NSEnumerator )
最新推荐文章于 2023-10-07 14:20:26 发布