数组

用来存贮对象的有序列表,它是不可变的

不能存数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);

}

最新文章

  1. Android知识杂记(四)
  2. 新建一个angularjs+requirejs+bootstrap+typescript+gulp+vscode+git的项目
  3. 使用SerialPort&#160;读取外置GPS信息和使用GeoCoordinateWatcher获取内置gps的信息
  4. 在国内时,更新ADT时需要配置的
  5. Windows重新建立图标缓存
  6. SSE求解向量大小
  7. 把虚拟机中的Linux系统安装到U盘中
  8. 最强PostMan使用教程(1)
  9. JS分号 惹的坑
  10. [python3.5][PyUserInput]模拟鼠标和键盘模拟
  11. android---EventBus的简单使用(一)
  12. Web API的接口访问安全性
  13. Python—UI自动化完整实战
  14. Codeforces Round #109 (Div. 1) 题解 【ABC】
  15. cmake 常用变量和常用环境变量查表手册---整理 .
  16. 《大话设计模式》c++实现 状态模式
  17. jackson的小知识
  18. jzoj5864
  19. Stack Overflow访问问题的处理
  20. java 内省(Introspector)

热门文章

  1. jacascript 数组
  2. CentOS下 .Net Core 1.0 升级到 3.0 遇到的一个小问题
  3. Python笔记-备忘
  4. vue 2.0 + 如何实现加入购物车,小球飞入的动画
  5. 查看mysql的bin-log日志
  6. 虚拟机和hadoop
  7. python 制作影视动画、电影特效工具
  8. Free lunch is over
  9. 目标检测 — two-stage检测
  10. Juit