#import <Foundation/Foundation.h>标签:stu2,stu1,NSLog,void,OC,使用,NSMutableArray,array From: https://blog.51cto.com/u_15907570/5925307
#import "Student.h"
void
NSMutableArray *array = [NSMutableArrayarrayWithObject:@"1"];
// 添加元素
addObject:@"2"];
addObject:@"3"];
// [array removeObject:@"2"];
// [array removeLastObject];
[array removeAllObjects];
NSLog(@"%@", array);
}
void
NSMutableArray *array = [[NSMutableArrayalloc] init];
// stu1:1
Student *stu1 = [[Studentalloc] init];
age =10;
// stu2:1
Student *stu2 = [[Studentalloc] init];
age =20;
//对被添加的元素做一次retain操作,计数器+1
addObject:stu1]; // stu1:2
addObject:stu2]; // stu2:2
NSLog(@"add->stu1:%zi", [stu1retainCount]);
// 对被删除的元素做一次release操作,计数器-1
removeObject:stu1]; // stu1:1
NSLog(@"remove->stu1:%zi", [stu1retainCount]);
// 释放学生
release]; // stu1:0
release]; // stu2:1
//当数组被释放的时候,会对所有的元素都做一次release操作
release]; // stu2:0
}
void
NSMutableArray *array = [NSMutableArrayarrayWithObjects:@"1",@"2", @"3", nil];
[array replaceObjectAtIndex:1withObject:@"4"];
NSLog(@"%@", array);
}
//数组排序
void
NSMutableArray *array = [NSMutableArrayarrayWithObjects:@"1",@"3", @"2", nil];
sortUsingSelector:@selector(compare:)];
NSLog(@"%@", array);
}
int main(int argc,const char
{
@autoreleasepool {
arraySort();
}
return 0;
}