OC语言基础之NSArray
0.数组的分类
NSArray :不可变数组
NSMutableArray : 可变数组
1: // @[] 只创建不可变数组NSArray
2: /* 错误写法
3: NSMutableArray *array = @[@"jack", @"rose"];
4:
5: [array addObject:@"jim"];
6: */
7:
8:
9: //NSArray *array = @[@"jack", @"rose"];
1.数组的创建
1: // char s[8];
2: // NSArray
3: // c语言里数组只能放定义时类型元素
4: // NSArray 可用放置各种类型对象
5: //NSArray *array = [NSArray array];
6: //NSArray *array1 = [NSArray arrayWithObject:@"12"];
7: NSArray *array2 = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
8: //NSArray *array3 = [NSArray arrayWithArray:array2];
9:
10:
11: //NSUInteger count = [array2 count];
12: //BOOL isContain = [array2 containsObject:@"22"];
13: // NSLog(@"%d", isContain);
14: //id lastObj = [array2 lastObject];
15:
16: // char s[8];
17: // s[1]
18: // id obj = [array2 objectAtIndex:0];
19: // NSLog(@"%@", obj);
20: // NSUInteger index = [array2 indexOfObject:@"2"];
21: // NSLog(@"%zd", index);
22:
23: // NSUInteger index = [array2 indexOfObject:@"3" inRange:NSMakeRange(0, 3)];
24: // if (NSNotFound == index) {
25: // NSLog(@"在0,2没有找到");
26: // }else{
27: // NSLog(@"找到了");
28: // }
2.数组的常用操作
1)可变数组
1: // NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:10];
2: //
3: NSMutableArray *array = [NSMutableArray array];
4: NSArray *array1 = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
5: [array setArray:array1];
6:
7: [array addObject:@"4"];
8:
9: // 1,2,3,4
10: // [array removeObject:@"4"];
11: // [array removeObjectAtIndex:3];
12: // [array removeAllObjects];
13:
14: // [array insertObject:@"11" atIndex:1];
15: // 1 11 2..
16: // [array removeLastObject];
17: // NSLog(@"%@", array);
18: // [array replaceObjectAtIndex:0 withObject:@"11"];
19:
20: [array exchangeObjectAtIndex:0 withObjectAtIndex:1];
21: NSLog(@"%@", array);
2)读写文件
1: // 写入文件
2: // NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
3: // NSString *path = @"//Users/apple/Desktop/1.txt";
4: // BOOL isSucess = [array writeToFile:path atomically:YES];
5: // if (isSucess) {
6: // NSLog(@"写入成功");
7: // }else{
8: // NSLog(@"写入失败");
9: // }
10:
11: NSString *path = @"//Users/apple/Desktop/1.txt";
12: NSArray *array = [NSArray arrayWithContentsOfFile:path];
13: NSLog(@"%@", array);
3)和NSString相关的操作
1: NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
2: NSString *str = [array componentsJoinedByString:@"="];
3:
4: // 1=2=3
5: NSArray *array1 =[str componentsSeparatedByString:@"="];
6: NSLog(@"%@", array1);
4)数组的截取
1: NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
2: NSArray *array1 = [array arrayByAddingObject:@"4"];
3: //NSLog(@"%@ ===== %@", array, array1);
4: NSArray *array2 = [array subarrayWithRange:NSMakeRange(0, 2)];
5: NSLog(@"%@", array2);
5)数组的遍历
1: //NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
2:
3: // 普通遍历
4: // for (int i=0; i<array.count; i++) {
5: // id obj = [array objectAtIndex:i];
6: // NSLog(@"%@", [obj description]);
7: //
8: // if (1 == i) {
9: // break;
10: // }
11: // }
12:
13: // 快速遍历
14: // int i = 0;
15: // for (id obj in array) {
16: // NSLog(@"%@", [obj description]);
17: //
18: // i++;
19: // if (2==i) {
20: // break;
21: // }
22: // }
23:
24: // block遍历
25: // [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
26: // NSLog(@"%@", [obj description]);
27: //
28: // if (1==idx) {
29: // (*stop) = YES;
30: // }
31: // }];
32:
33: Dog *dog = [[Dog alloc] init];
34: Dog *dog1 = [[Dog alloc] init];
35: NSArray *array = [NSArray arrayWithObjects:dog, dog1, nil];
36: [array makeObjectsPerformSelector:@selector(eat:) withObject:@"骨头"];
4)可变数组的基本使用
1: NSMutableArray *array = [NSMutableArray arrayWithObjects:@"rose", @"jim", nil];
2:
3: // 添加元素
4: [array addObject:[[Person alloc] init]];
5:
6: [array addObject:@"jack"];
7:
8: // 删除元素
9: //[array removeAllObjects];
10: // 删除指定的对象
11: // [array removeObject:@"jack"];
12: [array removeObjectAtIndex:0];
13:
14: // 错误写法
15: // [array addObject:nil];
16:
17:
18: NSLog(@"%@", array);
19:
20: NSLog(@"%ld", array.count);
5)其他的一些方法
1:
2: /*
3: 1.NSArray的创建
4: */
5: NSArray *array2 = [NSArray arrayWithObject:@"jack"];
6:
7: // nil是数组元素结束的标记
8: NSArray *array3 = [NSArray arrayWithObjects:@"jack", @"rose", nil];
9: // [array2 count];
10:
11: //NSArray *array4 = [NSArray arrayWithObjects:@"jack", @"rose", @"4324324", nil];
12:
13: // 快速创建一个NSArray对象
14: NSArray *array4 = @[@"jack", @"rose", @"4324324"];
15:
16:
17: /*
18: 2.NSArray的元素个数
19: */
20: NSLog(@"%ld", array3.count);
21:
22:
23: /*
24: 3.NSArray中元素的访问
25: */
26: NSLog(@"%@", [array3 objectAtIndex:1]);
27:
28: //array3[1];
29: NSLog(@"%@", array3[0]);