可变的NSArray,可以随意添加OC对象

 
1.创建
 void arrayCreate()
{
NSMutableArray *array = [NSMutableArray arrayWithObject:@""]; [array addObject:@""];
[array addObject:@""]; [array removeObject:@""];
NSLog(@"%@", array);
}
 
NSMutableArray 不能使用@[]创建
 
 
2.内存管理
当Array release的时候,里面的元素也会release一次
 void memoryManage()
{
NSMutableArray *array = [NSMutableArray array];
Student *stu1 = [Student initWithAge:];
Student *stu2 = [Student initWithAge:]; //Will retain stu1 one time automatically
[array addObject:stu1];
[array addObject:stu2]; NSLog(@"add--> stu1: %zi", [stu1 retainCount]); //Will release stu1 one time automatically
[array removeObject:stu1];
NSLog(@"remoe--> stu1: %zi", [stu1 retainCount]); NSLog(@"%@", array); //All element will be released one time
[array release];
}
 
3.替换元素
 void replaceArray()
{
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"", @"", @"", nil];
[array replaceObjectAtIndex: withObject:@"a"];
NSLog(@"%@", array);
}
 
4.排序
 void arraySort()
{
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"", @"", @"", nil];
[array sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%@", array);
}
 
5.删除元素
         NSMutableArray *a = [NSMutableArray array];
[a addObject:@];
[a addObject:@];
[a removeObject:@];
NSLog(@"%@", a);
[a removeAllObjects];
NSLog(@"%@", a);
 
 
04-29 04:12