NSArray 的创建和遍历
数组
用来存贮对象的有序列表,它是不可变的
不能存数C语言的基本数据类型
只支持OC对象
#pragma mark Create a array
//Initialize NSArray
void arrayCreate(){
//Create an empty array
NSArray *array=[NSArray array];// static method create static Array no need to release
//empty can’t add boz it can’t be changed
//Create a array with one object
array=[NSArray arrayWithObject:@”123”];
//nil 代表我们元素的结束
array=[NSArray arrayWithObjects:@“a”,@”b”,@”c”,nil];
//3 elements. You can’t add a nil element, nil represent end of the array
//All above three methods need no release.
NSArray arrar1=[[NSArray alloc]init];
[array1 release];
//Check how many elements in array
unsigned int count =[array count]; //count is a get method ==
//unsigned int count=array.count;
NSLog(@”%i”,count);
}

#pragma mark use the array
void arrayUse(){
//Whether contain certain element
array=[NSArray arrayWithObjects:@“a”,@”b”,@”c”,nil];
if([array containsObject:L@”a”]){
NSLog(@”Contain a”);
}
//Return last element
NSString *last=[array lastObject];
NSLog(@”last is %@”,last);
//Get element from certain position
NSLog(@” Element in postion 1 is %@”,[array objectAtIndex:1]);
//Search the position of certain object ( and with range)
NSLog(“Positon of c is %i”,[array indexOfObject:@”c”]);
NSObject *obj=[[[NSObject alloc]init]autorelease];
array1=[NSArray arrayWithObjects:@“a”,@”b”,@”c”,@”a”,obj,nil];
NSRange range=NSMakeRange(0,2);
NSLog(“Positon of a is %i”,range);
}
//put Student Object
Student.h
-(void)dealloc{
NSLog(@”%@ is destroied”,self);
[super dealloc];
}
Student.m
#import “Student.h”
// pragma mark memory management about array addition
void arrayMemory(){
//If u insert an Object into an array , counter will retain once
Student *stu1=[[Student dealloc]init]; //counter =1
Student *stu2=[[Student dealloc]init];
//counter =1
Student *stu3=[[Student dealloc]init];
//counter =1
NSArray array=[[NSArray alloc]initWithObjects:stu1,stu2,stu3,nil];//counter=2
NSLog(@”count=%zi”,array.count);
NSLog(@”counter of stu1 ,stu2,stu3 is %zi, %zi, %zi”,[stu1 retainCount],[stu2 reytainCount],[stu3 retainCount]);// 2
[array release];
//When an array is deroied , all object counter inside the array will be release once
[stu1 release];
[stu2 release];
[stu3 release];
}
//NSArray 的比较
-(BOOL)isEqualToArray:(NSarray *)otherArray
//比较两个数组的内容是否相同
-(id)firstObjectCommonWithArray:(NSarray *)otherArray
//return the first same Object of two Array
#pragma mark send message to Object in Array
//-(void)makeObjectsPerformSelector:(SEL)aSelector
//-(void)makeobjectsperformSelector:(SEL)aSelector withobject:(id)argument
//让数组里面所有元素都执行aSelector 这个方法
Student.h
@interface Student:NSobject
-(void)test;
-(void)test2:(NSString *)str;
+(id)student;
@end
Student.m
@implementation
+(id)student{
return [[[Student alloc]init]autorelease];
}
-(void)test{
NSLog(@“%@->test”,self);
}
-(void)test2:(NSString *)str{
NSLog(@”%@->%@”,self,str);
}
-(void)dealloc{
NSLog(@”%@ is destroied”,self);
}
@end
#pragma mark Send message to element inside array
main.m
void arrayMeaagae(){
Student *stu1=[Student student]; //+ no nee release
Student *stu2=[Student student];
Student *stu3=[Student student];
NSArray *array=[NSArray arrayWithObjects:stu1,stu2,stu3,nil]; //+
//All the object call method test
[array makeObjectsPerformSelector:@selector(test)];
//All the object call method test with a parameter
[array makeObjectsPerformormSelector:@selector(test2:)withObject:@”123”];
}
//pragma mark traverse array
void arrayFor1(){
Student *stu1=[Student student];
NSArray *array=[NSArray arrayWithObjects:stu1,@”1”,@”2”,@”3”,nil];
//id==void *
int count=array.count;
for(int i=0;i<count;i++){
id obj=[array objectAtIndex:i];
NSLog(@”%i->%@”,i,obj);
}
}
//pragma mark traverse array2
void arrayFor2(){
Student *stu1=[Student student];
NSArray *array=[NSArray arrayWithObjects:stu1,@”1”,@”2”,@”3”,nil];
//id==void *
int i=0;
for(id obj in array){
NSLog(@”%i->%@”,i,obj);
if(i==1)
break;
i++;
}
}
//pragma mark traverse array3 block
void arrayFor3){
Student *stu1=[Student student];
NSArray *array=[NSArray arrayWithObjects:stu1,@”1”,@”2”,@”3”,nil];
[array enumerateObkectUsingBlock:
^(id obj,NSUInteger idx,BOOL *stop){
NSLog(@”%i->%@”,idx,obj);
if(idex==1){
*stop=YES;} //stop traverse
}
];
}
//Tips: 平时写代码dealloc方法中最好打印一下,这样可以看出对象有没有被销毁,然后可以检测有没有内存泄露。
#pragma mark NSEnumerator
//集合的迭代器,可用于遍历集合元素
//NSArray有相应的方法来可以获取迭代器
//-(NSEnumerator *)objectenumerator
//获取一个正的迭代器
//-(NSEnumerator *)reverseobjectenumerator
//获取一个反序遍历的迭代器
//常用方法
//-(id)nextobject
//attain next object
//-(NSArray *)allobjects
//attatain all objects
//pragma mark traverse array4 enumerator
void arrayFor4(){
Student *stu1=[Student student];
NSArray *array=[NSArray arrayWithObjects:stu1,@”1”,@”2”,@”3”,nil];
NSEnumerator *enumerator=[array objectEnumerator];
//NSEnumerator *enumerator=[array reverseObjectEnumerator];
id obj=nil;
while(obj=[enmurator nextObject]){
NSLog(@”obj=%@”,obj);
}
NSArray array2=[enumerator allObjects];
//Should put in frond of traverse otherwise nil
//它是取出没有被遍历过得对象
NSLog(@”array2:%@”,array2);
}
NSArray 的创建和遍历的更多相关文章
- [OC Foundation框架 - 7] NSArray的创建与遍历
NSArray是不可变的,不能先创建再添加元素 NSArray可以放入任何OC对象,但不能放入基本数据类型.结构体.枚举等非OC对象 不能存储nil A.常用方法1 创建 返回用量 是否含有某元素 ...
- NSArray与NSMutableArray 数组与可变数组的创建和遍历 复习
1.NSArray 是一个父类,NSMUtableArray是其子类,他们构成了OC的数组. 2.NSArray的创建 NSArray * array = [[NSArray alloc]initWi ...
- C++ 创建和遍历二叉树
一个简单的创建和遍历二叉树的C++程序,二叉树的其他操作程序待更新. #include <iostream> using namespace std; struct BiTNode{ ch ...
- 图的创建和遍历(BFS/DFS)
图的表示方法主要有邻接矩阵和邻接表.其中邻接表最为常用,因此这里便以邻接表为例介绍一下图的创建及遍历方法. 创建图用到的结构有两种:顶点及弧 struct ArcNode { int vertexIn ...
- c/c++二叉树的创建与遍历(非递归遍历左右中,破坏树结构)
二叉树的创建与遍历(非递归遍历左右中,破坏树结构) 创建 二叉树的递归3种遍历方式: 1,先中心,再左树,再右树 2,先左树,再中心,再右树 3,先左树,再右树,再中心 二叉树的非递归4种遍历方式: ...
- c/c++叉树的创建与遍历(非递归遍历左右中,不破坏树结构)
二叉树的创建与遍历(非递归遍历左右中,不破坏树结构) 创建 二叉树的递归3种遍历方式: 1,先中心,再左树,再右树 2,先左树,再中心,再右树 3,先左树,再右树,再中心 二叉树的非递归4种遍历方式: ...
- python创建与遍历List二维列表
python创建与遍历List二维列表 觉得有用的话,欢迎一起讨论相互学习~Follow Me python 创建List二维列表 lists = [[] for i in range(3)] # 创 ...
- JS实现图的创建和遍历
图分为无向图和有向图 图的存储结构有邻接矩阵.邻接表.十字链表.邻接多重表这四种,最常用的是前两种 本篇主要是利用邻接矩阵实现无向图的创建和遍历(深度优先.广度优先),深度优先其实就是二叉树里的前序遍 ...
- [数据结构】【c语言】链表的创建和遍历
第一次写代码的博客,一个刚刚接触的新手,来这里主要是为了记录自己,方便自己以后浏览,也欢迎大家指正.先来个简单的,动态链表的创建和遍历. #include<stdio.h> #includ ...
随机推荐
- 安装本地jar包
(1)安装在本地maven库 假设我们需要引入的包为 myjar-1.0.jar (1.1)打开cmd,进入myjar-1.0.jar所在的目录 (1.2)执行如下命令:mvn install:ins ...
- springboot 整合 web 项目找不到 jsp 文件
今天遇到一个问题,就是使用springboot整合web项目的时候,怎么都访问不到 \webapp\WEB-INF\jsp\index.jsp 页面.这个问题搞了半天,试了各种方式.最后是因为在启动的 ...
- (五)Hibernate的增删改查操作(2)
接上一章节 HQL的预编译语句 HIbernate中的预编译与Spring的预编译的处理差不多. 1:使用标准的? 2:使用命名参数 2.1:使用名称逐个设置. 2.2:使用Map(k ...
- (三)调用web服务
(二)发布第一个WebService服务与DSWL文档解析讲解了如何发布一个web服务,本章主要讲述如何调用一个web服务. 这里有三种方式: 使用代理模式调用,需要将服务端的接口类拷贝到客户端中.( ...
- SqlServer用sql语句清理log日志
原文:SqlServer用sql语句清理log日志 USE[master] ALTER DATABASE [Center] SET RECOVERY SIMPLE WITH NO_WAIT ALTER ...
- 基于【 centos7】四 || FastDFS集群+Nginx负载均衡
1. 架构设计 1.1 架构图 FastDFS是用c语言编写的一款开源的分布式文件系统.FastDFS为互联网量身定制,充分考虑了冗余备份.负载均衡.线性扩容等机制,并注重高可用.高性能等指标,使用F ...
- 关于微信小程序使用watch监听数据变化的方法
众所周知,Vue中,可以使用监听属性 watch来观察和响应 Vue 实例上的数据变化,那么小程序能不能实现这一点呢? 监听器的原理,是将data中需监听的数据写在watch对象中,并给其提供一个方法 ...
- vue+scss动态改变主题颜色
1.新建.scss后缀公用文件,放在assets或者其他地方都可以 /*需要切换的颜色变量*/ $color-primary1:#1776E1; /* 更换的颜色 */ $color-primary2 ...
- flask 中的ORM
1 响应(response) 1 什么是响应 响应就是由服务器端带给客户端的内容,对应着请求,响应可以是普通的字符串,模板 或重定向 return '普通字符串' return render_temp ...
- CentOS7.X 搭建LAMP
第一部分搭建LAMP基础环境 1.检查CentOS是否为7.x版本 2.安装LAMP中的apache,采用yum源方法安装 yum install httpd httpd-devel A ...