博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NSArray数组(1)
阅读量:2352 次
发布时间:2019-05-10

本文共 2307 字,大约阅读时间需要 7 分钟。

 //统称为集合类,都是用来打包对象的

NSArray 数组          NSDictionary 字典            NSSet  集合

能够做什么:

1:添加,删除,插入,取出

2:创建

3:遍历集合

4:排序

 对于NSArray(数组中存储的实际上是对象的指针地址)

》1:创建集合,NSArray是不可变数组,一旦创建完成就不能够对数组进行删除、添加等操作

NSArray *array = [[NSArray alloc]init];

NSLog(@"%@",array);  //  打印为  ()

》2:通过构造方法的方式创建一个NSArray,在创建一个NSArray的时候,集合的最后一个元素一定是nil

NSArray  *array1 = [[NSArray arrayWithObjects:@"one",@"two",@"three",nil];

NSLog(@"%@",array);  //  打印为  (one, two,three)

》3:数组中可以存储不同类型的对象

NSNumber  *number = [NSNumber numberWithInt:10];

NSArray *array2 = [[NSArray  alloc]initWithObjects:@"one",@"two",number,nil];

NSLog(@"%@",array);  //  打印为  (one, two,10)

》4:数组实际上存储的是对象的地址,同样也可以存储数组的地址

NSArray *a1 = [[NSArray  alloc]initWithObjects:@"one",@"two",@"three",nil];

NSArray *a2 = [[NSArray  alloc]initWithObjects:@"1",@"2",@"3",nil];

NSArray *a3 = [[NSArray  alloc]initWithObjects:a1,a2,nil];

NSLog(@"%@",a3);  //  打印为  ((one, two,three),(1,2,3))   

》5:存储自定义的对象,最好都重写description方法

先创建一个继承自NSObject 的类Person

在Person.m中

-(id)initWithName:(NSString *)name andAge:(int)age

{

   if(self = [super init])

  {

     _name = name;

    _age = age;

  }

return  self;

}

-(NSString *)description

{

    return  [NSString stringWithFormat:@"age= %d,  name = %@",_age,_name);

}

#import "Person.h" 之后

Person *p1 = [[Person alloc]initWithName:@"zhong"  andAge:22];

Person *p2 = [[Person alloc]initWithName:@"lili"  andAge:20];

Person *p3 = [[Person alloc]initWithName:@"meiz"  andAge:30];

NSArray *array = [[NSArray alloc]initWithObjects:p1,p2,p3,nil];

NSLog(@"%@",array];  //结果为  ( " age=22,name=zhong"," age=20,name=lili"," age=30,name=meiz")

》6:数组中存储基本数据类型,应将基本数据类型包装好之后再存储。

  注:不要把nil值存储到NSArray中,会导致数据丢失

NSString *str = nil;

NSArray *array = [[NSArray alloc]initWithObject :@"2",str,[NSNumber numberWithInt:4],nil];

》7:创建数组的快捷方式

NSArray  *karray = @[@"b",@"c",@"d"];

》8:快速获得一个数组中的元素

NSString *kstr = karray[0];

NSLog(@"%@",kstr];

》9:从集合中取出数据

NSArray *array = [[NSArray  alloc]initWithObjects:@"one",@"two",number,nil];

NSString *str = [array objectAtIndex:0];  //数组的index是从0开始的

》10:获得数组的元素个数

NSUInteger arrayCount = [array count];

NSLog(@" %d" ,arrayCount);  //  3

》11:判断数组中是否存在某个对象
Person *p1 = [[Person alloc]initWithName:@"zhong"  andAge:22];
Person *p2 = [[Person alloc]initWithName:@"y"  andAge:22];
   
 NSArray *array = @[p1,p2];

 BOOL isContain = [array containsObject:p1];

if(isContain)

{

   NSLog(@“存在”);

}

else{

 NSLog(@“不存在”);

}

转载地址:http://lxrvb.baihongyu.com/

你可能感兴趣的文章
信用卡反欺诈
查看>>
线性回归
查看>>
浏览器以只读方式打开PDF
查看>>
CDH和HDP下载地址
查看>>
MysqlDataTruncation: Data truncation: Incorrect string value: '\xF0\x9D\x90\xB6"#...' for column
查看>>
.MysqlDataTruncation: Data truncation: Data too long for column 'content' at row 1
查看>>
com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1146177 > 1048576).
查看>>
Elasticsearch 7.x生产配置
查看>>
AccessDeniedException: /opt/elasticsearch-7.0.0/config/elasticsearch.keystore
查看>>
bootstrap-table 父子表 联动表 完整例子
查看>>
Spring Cloud 2.x完整入门Demo样例(Greenwich版本)
查看>>
Spring Cloud 2.x学习笔记:2、feign改进(Greenwich版本)
查看>>
SpringCloud 2.x学习笔记:3、Hystrix(Greenwich版本)
查看>>
SpringCloud 2.x学习笔记:4、Zuul(Greenwich版本)
查看>>
ajax提交JSON数组及Springboot接收转换为list类
查看>>
SpringCloud 2.x学习笔记:5、Config(Greenwich版本)
查看>>
RabbitMQ安装、配置与入门
查看>>
Java异常
查看>>
Ibatis代码自动生成工具
查看>>
ant build.xml教程详解
查看>>