zoukankan      html  css  js  c++  java
  • objectc中NSArray 的操作(1)

    下图是要展示NSArray集合操作的一部分API

    1:首先提供student.h

    //
    //  Student.h
    //  NSArray复习01
    //
    //  Created by ganchaobo on 13-4-25.
    //  Copyright (c) 2013年 ganchaobo. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    typedef void (^MySum)(id);
    
    @interface Student : NSObject
    
    @property(nonatomic ,retain) NSString * name;
    
    -(id)InitWithName:(NSString *) Name;//自定义动态构造函数(模仿系统)
    +(id)StudentWithName:(NSString *) Name;//自定义静态构造方法 模仿系统)
    -(void)print;
    -(void)print:(id) msg;
    -(void)Test:(void (^)(id aa)) block ;
    
    
    @end
    

    2:在提供student.m

    //
    //  Student.m
    //  NSArray复习01
    //
    //  Created by ganchaobo on 13-4-25.
    //  Copyright (c) 2013年 ganchaobo. All rights reserved.
    //
    
    #import "Student.h"
    
    @implementation Student
    @synthesize name=_name;
    
    -(id)InitWithName:(NSString *)Name{
        self=[super init];
        if(self){
            self.name=Name;
        }
        return self;
    }
    
    +(id)StudentWithName:(NSString *)Name{
        return  [[[Student alloc] InitWithName:Name] autorelease];
    }
    
    -(void)dealloc{
        NSLog(@"Student 对象被销毁%@",self);//此时发送descript方法
        [super dealloc];
    }
    
    #pragma mark - 重写descript方法
    -(NSString *)description{
        return [NSString stringWithFormat:@"name-->%@",self.name];
    }
    
    -(void)print{
        NSLog(@"print -->%@",self.name);
    }
    
    -(void)print:(id)msg{
        NSLog(@"print:--->%@ -->%@",msg,self.name);
    }
    
    -(void)Test:(void (^)(id))block{//匿名block 对象
    //    MySum MM=block;
    //    MM(@"11");
    //    block=^(id aa){
    //        NSLog(@"%@",self);
    //    };
        block(self.name);
    }
    @end
    

    3:NSArray的操作

    //
    //  main.m
    //  NSArray复习01
    //
    //  Created by ganchaobo on 13-4-25.
    //  Copyright (c) 2013年 ganchaobo. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import "Student.h"
    //NSArray管理对象内存机制 即对象存放到NSArray里面的时候 对象retain一次,当对象从NSArray中移除的时候 对象release一次
    void NSArrayObjectCount(){
    
        Student *stu1=[Student StudentWithName:@"ABC"];
    
            NSLog(@"student--->%@,计数器%zi",stu1,[stu1 retainCount]);
        
        NSArray *arr=[[NSArray alloc] initWithObjects:stu1, nil] ;//第一中创建NSArray方法
        
        NSLog(@"student--->%@,计数器%zi",stu1,[stu1 retainCount]);
        
        NSLog(@"before arr release");
        [arr release];
            NSLog(@"after arr release");
        
        NSLog(@"student--->%@,计数器%zi",stu1,[stu1 retainCount]);
        
    }
    //创建NSArray的方法 :NSArray是有序的集合,不可变
    void CreateArray(){
        //第一种创建NSSArray 方法
        NSArray *arr1=[[[NSArray alloc] initWithObjects:@"123",@"123",nil] autorelease];//NSArray 里面只能放obejct-c对象,其中nil只是NSArray集合结尾的标识,没有实际意思
        NSLog(@"%@",arr1);
        //第二种创建NSArray方法
        NSArray *arr2=[NSArray arrayWithObjects:@"234",@"345", nil];
        NSLog(@"%@",arr2);
        //第三种创建NSArray 对象,但是只有只有一个集合的NSArray
        NSArray *arr3=[NSArray arrayWithObject:@"123"];
        NSLog(@"%@",arr3);
        
        
    }
    
    //NSArray的查询
    void NSArrayQuery(){
        NSArray *arr=[NSArray arrayWithObjects:@"abc",@"Act",@"AED",@"AFG", nil];
        NSLog(@"%zi",[arr count]);//计算NSArray集合的个数
        NSLog(@"%@",[arr lastObject]);//求NSArray集合种的最后一个元素
        NSLog(@"%@",[arr objectAtIndex:1]);//根据索引得到指定的对象
        NSLog(@"%i",[arr containsObject:@"abc"]);//是否包含某个元素
        NSLog(@"%zi",[arr indexOfObject:@"AED"]);//匹配对象返回对象所在集合的索引
        NSLog(@"%zi",[arr indexOfObject:@"AFG" inRange:NSMakeRange(2, 2)]);
    
        //集合比较
        NSLog(@"集合");
        NSArray *arr1=[NSArray arrayWithObjects:@"AA",@"Act",@"AED",@"AFG", nil];
        NSLog(@"%zi",[arr1 isEqualToArray:arr]);//比较两个集合是否相同
        NSLog(@"%@",[arr1 firstObjectCommonWithArray:arr]);//返回两个集合种第一个相同的对象
    }
    //给NSArray发送消息
    void NSArraySend(){
        Student *stu1=[Student StudentWithName:@"JACK"];
         Student *stu2=[Student StudentWithName:@"Mike"];
        NSArray *arr=[NSArray arrayWithObjects:stu1,stu2, nil];
        
        [arr makeObjectsPerformSelector:@selector(print)];//让集合中的每个对象执行print 方法
        [arr makeObjectsPerformSelector:@selector(print:) withObject:@"Good"];
        
    }
    //第一种排序:经典for循环
    void ArrayFor1(){
            NSArray *arr=[NSArray arrayWithObjects:@"AA",@"Act",@"AED",@"AFG", nil];
       NSUInteger count= [arr count];
        for(int i=0;i<count;i++){
            id obj=[arr objectAtIndex:i ];
            NSLog(@"index=%i,%@",i,obj);
        }
    }
    //第二种排序:c#种foreach
    void ArrayFor2(){
        NSArray *arr=[NSArray arrayWithObjects:@"AA",@"Act",@"AED",@"AFG", nil];
        for( id obj in arr){
            NSLog(@"%@",obj);
        }
    }
    //第三种排序:迭代器
    void ArrayFor3(){
        NSArray *arr=[NSArray arrayWithObjects:@"AA",@"Act",@"AED",@"AFG", nil];
        id obj=nil;
        NSEnumerator *enumerator= [arr objectEnumerator];//得到集合的迭代器
        while(obj=[enumerator nextObject])
        
        {
            NSLog(@"%@",obj);
        }
    
    }
    //第4中排序:迭代器(1)倒序迭代器
    
    void ArrayFor4(){
        NSArray *arr=[NSArray arrayWithObjects:@"AA",@"Act",@"AED",@"AFG", nil];
        id obj=nil;
        NSEnumerator *enumerator=[arr reverseObjectEnumerator];//得到集合的倒序迭代器
        while(obj=[enumerator nextObject]){
            NSLog(@"%@",obj);
        }
        
    }
    
    //第5种排序 ^block
    void ArrayFor5(){
        NSArray *arr=[NSArray arrayWithObjects:@"AA",@"Act",@"AED",@"AFG", nil];
    //    [arr enumerateObjectsUsingBlock:<#^(id obj, NSUInteger idx, BOOL *stop)block#>]
        [arr enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop){
            NSLog(@"%@",obj);
        }];
    }
    
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
          
            Student *STU=[Student StudentWithName:@"MIKE"];
            [STU Test:^(id str){
                NSLog(@"%@",str);//blcok的机
            }];
           // ArrayFor4();
            
        }
        return 0;
    }
    

    NSArray其他函数的操作

     

    //NSArray中的迭代器
    void Enumerator(){
            NSArray *arr=[NSArray arrayWithObjects:@"123",@"234",@"567", nil];
        NSEnumerator * Enumerator= [arr  objectEnumerator];//得到集合的迭代器
          NSLog(@"%@",[Enumerator nextObject]);//获取迭代器的下一个元素
       NSLog(@"%@",[Enumerator allObjects]);//通过迭代器获取NSArray的元素
      
        
    }
    //NSArray中派生出来新的类
    void MustArray(){
          NSArray *arr=[NSArray arrayWithObjects:@"123",@"234",@"567", nil];//arr指向的数组是不可变的。
        NSArray *arr1=[arr arrayByAddingObject:@"ABC"];//添加一个元素返回新的元素,arr元素没有变化(笔者猜测里面是用指针操作)
        NSLog(@"%@",arr1);
        
        NSArray *arr2=[NSArray arrayWithObjects:@"AC",@"AD",@"AE", nil];//arr2指向的数组是不可变的。
        NSArray *arr3=[arr2 arrayByAddingObjectsFromArray:arr];//添加一个数组,返回新的数组
        NSLog(@"%@",arr3);
        
        NSArray *arr4=[arr2 subarrayWithRange:NSMakeRange(1, 2)];
        NSLog(@"%@",arr4);
        
    }
    //NSArray的其他操作
    void otherArray(){
    NSArray *arr2=[NSArray arrayWithObjects:@"AC",@"AD",@"AE", nil];//arr2指向的数组是不可变的。
        NSString *str=[arr2 componentsJoinedByString:@"-"];//将一个NSArray已“-”分割符号拼接成一个字符串
        NSLog(@"%@",str);
        
    }
    
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
        
    //        Enumerator();
            //MustArray();
            otherArray();
        }
        return 0;
    }
    
  • 相关阅读:
    结巴分词 0.14 版发布,Python 中文分词库
    Lazarus 1.0.2 发布,Pascal 集成开发环境
    Android全屏 去除标题栏和状态栏
    服务器日志现 Android 4.2 传将添多项新特性
    Percona XtraBackup 2.0.3 发布
    长平狐 Android 强制设置横屏或竖屏 设置全屏
    NetBeans 7.3 Beta 发布,全新的 HTML5 支持
    CppDepend现在已经支持Linux
    GromJS 1.7.18 发布,服务器端的 JavaScript
    Apache OpenWebBeans 1.1.6 发布
  • 原文地址:https://www.cnblogs.com/gcb999/p/3045705.html
Copyright © 2011-2022 走看看