首页 > 其他分享 >OC之【NSMutableArray的使用】

OC之【NSMutableArray的使用】

时间:2022-12-09 15:04:52浏览次数:44  
标签:stu2 stu1 NSLog void OC 使用 NSMutableArray array

#import <Foundation/Foundation.h>
#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;
}

标签:stu2,stu1,NSLog,void,OC,使用,NSMutableArray,array
From: https://blog.51cto.com/u_15907570/5925307

相关文章

  • OC之【@protocol协议】
    #import<Foundation/Foundation.h>@protocolStudy<NSObject>默认就是@required-(void)test3;表示必须实现的方法//虽然字面上说是必须实现,但是编译器并不强求某个类......
  • OC之【@class】
    如果是继承某个类,就要导入类的头文件@classbook.h文件:#import<Foundation/Foundation.h>Book:NSObjectintprice;@end文件:"Book.h"Book-(void)dealloc{NSL......
  • OC之【深拷贝(mutableCopy)和浅拷贝(copy)】
    文件#import<Foundation/Foundation.h>#import"Student.h"#import"GoodStudent.h"//copy语法的目的:改变副本的时候,不会影响到源对象字符串的拷贝(深拷贝)//深拷贝:内容......
  • OC之【NSNumber的使用】
    #import<Foundation/Foundation.h>void//将int类型的10包装成一个NSNumber对象NSNumber*number=[NSNumbernumberWithInt:10];NSLog(@"number=%@",number);......
  • OC之【NSDictionary详解】
    main.m文件#import<Foundation/Foundation.h>#import"Student.h"字典的初始化void//NSDictionary是不可变的NSDictionary*dict=[NSDictionarydictionaryWit......
  • OC之【c语言结构体】
    <stdio.h>void//这个机构只能在函数内部使用//定义一个名为Student的结构体类型structintage;//年龄char*name;//姓名floatheight;//身高};......
  • OC之【NSMutableDictionary的使用】
    main.m文件#import<Foundation/Foundation.h>#import"Student.h"可变字典的使用void//创建一个空的字典NSMutableDictionary*dict=[NSMutableDictionarydicti......
  • OC之【NSValue的使用】
    #import<Foundation/Foundation.h>voidCGPointpoint=CGPointMake(10,10);//将结构体变量包装成一个对象NSValue*value=[NSValuevalueWithPoint:point];......
  • OC之【enum枚举】
    void//定义一种枚举类型enum//定义一个枚举变量senumSeasons=winter;}void//定义枚举类型的同时定义一个枚举变量senumSeason{spring,summer,......
  • OC之【NSDate使用】
    #import<Foundation/Foundation.h>日期创建void//date方法返回的就是当前时间(now)NSDate*date=[NSDatedate];//now:21:09:40//date:21:09:50......