创建类
数组类只能装OC对象,不能装C基本类型
当把一个对象加入一个数组时,这个对象引用计数会加1
数组被销毁时,会对内部的所有元素都做一次release操作
#pragma mark 创建一个数组
点击(此处)折叠或打开
1.
2. void arrayCreate() {
3. // 创建一个空的数组
4. NSArray *array = [NSArray array];
5.
6. // 创建有1个元素的数组
7. array = [NSArray arrayWithObject:@"123"];
8.
9. // 创建有多个元素的数组
10. array = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];
11.
12. int count = [array count];
13. // count = array.count;
14. NSLog(@"%i", count);
15. }
#pragma mark 数组的简单使用
点击(此处)折叠或打开
#pragma mark 数组的内存管理
1. void arrayUse() {
2. NSObject *obj = [[NSObject alloc] init];
3. NSArray *array = [NSArray arrayWithObjects:@"a", @"b", @"c" , obj, nil];
4. // 判断是否包含了某个元素
5. if ([array containsObject:@"a"]) {
6. NSLog(@"包含了字符串a");
7. }
8.
9. NSString *last = [array lastObject];
10. NSLog(@"last=%@", last);
11.
12. NSString *str = [array objectAtIndex:1];
13. NSLog(@"%@", str);
14.
15. int index = [array indexOfObject:@"c"];
16. NSLog(@"index=%i", index);
17.
18. [obj release];
19. }
点击(此处)折叠或打开
#pragma mark 给数组里面的元素发送消息
1. void arrayMemory() {
2. // 1
3. Student *stu1 = [[Student alloc] init];
4. Student *stu2 = [[Student alloc] init];
5. Student *stu3 = [[Student alloc] init];
6.
7. NSLog(@"stu1:%zi", [stu1 retainCount]);
8.
9. // 当把一个对象塞进数组中时,这个对象的计数器会加1,也就是说数组会对它做一次retain操作
10. // 2
11. NSArray *array = [[NSArray alloc] initWithObjects:stu1, stu2, stu3, nil];
12.
13. NSLog(@"stu1:%zi", [stu1 retainCount]);
14.
15. NSLog(@"count=%zi", array.count);
16.
17. // 1
18. [stu1 release];
19. [stu2 release];
20. [stu3 release];
21.
22. // 数组被销毁的时候,会对内部的所有元素都做一次release操作
23. // 0
24. [array release];
25. }
点击(此处)折叠或打开
1.
2. void arrayMessage() {
3. Student *stu1 = [Student student];
4. Student *stu2 = [Student student];
5. Student *stu3 = [Student student];
6.
7. NSArray *array = [NSArray arrayWithObjects:stu1, stu2, stu3, nil];
8. // 让数组里面的所有对象都调用test方法
9. // [array makeObjectsPerformSelector:@selector(test)];
10. [array makeObjectsPerformSelector:@selector(test2:) withObject:@"123"];
11. }
#pragma mark 遍历数组1
点击(此处)折叠或打开
1. void arrayFor1() {
2. Student *stu1 = [Student student];
3. NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil];
4. int count = array.count;
5. for (int i = 0; i<count; i++) {
6. // id == void *
7. id obj = [array objectAtIndex:i];
8. NSLog(@"%i-%@", i, obj);
9. }
10. }
#pragma mark 遍历数组2
点击(此处)折叠或打开
#pragma mark 遍历数组3
1. void arrayFor2() {
2. Student *stu1 = [Student student];
3. NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil];
4. // 快速遍历
5. int i =0;
6. for (id obj in array) {
7. NSLog(@"%i-%@", i, obj);
8. i++;
9. }
10. }
点击(此处)折叠或打开
1.
2. void arrayFor3() {
3. Student *stu1 = [Student student];
4. NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil];
5. [array enumerateObjectsUsingBlock:
6. ^(id obj, NSUInteger idx, BOOL *stop) {
7. NSLog(@"%zi-%@", idx, obj);
8.
9. // 如果索引为1,就停止遍历
10. if (idx == 1) {
11. // 利用指针修改外面BOOL变量的值
12. *stop = YES;
13. }
14. }];
15. }
#pragma mark 遍历数组4
点击(此处)折叠或打开
1. void arrayFor4() {
2. Student *stu1 = [Student student];
3. NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil];
4.
5. // 获取数组的迭代器
6. // NSEnumerator *enumerator = [array objectEnumerator];
7. // 反序迭代器(从尾部开始遍历元素)
8. NSEnumerator *enumerator = [array reverseObjectEnumerator];
9.
10. // allObjects是取出没有被遍历过的对象
11. NSArray *array2 = [enumerator allObjects];
12. NSLog(@"array2:%@", array2);
13.
14. // 获取下一个需要遍历的元素
15. id obj = nil;
16. while (obj = [enumerator nextObject]) {
17. NSLog(@"obj=%@", obj);
18. }
19. }
NSMutArray:
创建
点击(此处)折叠或打开
1.
2. void arrayCreate() {
3. NSMutableArray *array = [NSMutableArray arrayWithObject:@"1"];
4. // 添加元素
5. [array addObject:@"2"];
6. [array addObject:@"3"];
7.
8. // [array removeObject:@"2"];
9. // [array removeLastObject];
10. [array removeAllObjects];
11.
12. NSLog(@"%@", array);
13. }
内存管理
点击(此处)折叠或打开
1. void arrayMemory() {
2. NSMutableArray *array = [[NSMutableArray alloc] init];
3. // stu1:1
4. Student *stu1 = [[Student alloc] init];
5. stu1.age = 10;
6. // stu2:1
7. Student *stu2 = [[Student alloc] init];
8. stu2.age = 20;
9.
10. // 对被添加的元素做一次retain操作,计数器+1
11. [array addObject:stu1]; // stu1:2
12. [array addObject:stu2]; // stu2:2
13.
14. NSLog(@"add->stu1:%zi", [stu1 retainCount]);
15.
16. // 对被删除的元素做一次release操作,计数器-1
17. [array removeObject:stu1]; // stu1:1
18.
19. NSLog(@"remove->stu1:%zi", [stu1 retainCount]);
20.
21. // 释放学生
22. [stu1 release]; // stu1:0
23. [stu2 release]; // stu2:1
24.
25. // 当数组被释放的时候,会对所有的元素都做一次release操作
26. [array release]; // stu2:0
27. }
替换:
点击(此处)折叠或打开
排序:
1. void arrayReplace() {
2. NSMutableArray *array = [NSMutableArray arrayWithObjects:@"1", @"2", @"3", nil];
3.
4. [array replaceObjectAtIndex:1 withObject:@"4"];
5.
6. NSLog(@"%@", array);
7. }
点击(此处)折叠或打开
1. void arraySort() {
2. NSMutableArray *array = [NSMutableArray arrayWithObjects:@"1", @"3", @"2", nil];
3.
4. [array sortUsingSelector:@selector(compare:)];
5.
6. NSLog(@"%@", array);
7. }