zoukankan      html  css  js  c++  java
  • 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);

    }

    image


    #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);

    }

  • 相关阅读:
    截取图片组件
    node之mongodb的DAO
    模块化开发插件,组件
    tweenMax实体抛物线
    defineProperties属性的运用==数据绑定
    程序概述
    JavaBase
    [luogu 1092] 虫食算 (暴力搜索剪枝)
    [luogu1073 Noip2009] 最优贸易 (dp || SPFA+分层图)
    [51Nod 1218] 最长递增子序列 V2 (LIS)
  • 原文地址:https://www.cnblogs.com/yesihoang/p/4536396.html
Copyright © 2011-2022 走看看