IOS开发之路-Objective-C 集合上(笔记)

   先上昨天跟着老师写的东西吧,有例子比较明显.

之后还有一个实战练习.

数组的创建

        NSArray  *array = [NSArray arrayWithObjects:@"孙悟空",@"猪八戒",@"沙僧",nil];
        //NSArray 是不可变的,元素内容不能改变,而且是有序的

   

数组的使用

                //获取指定位置的元素
                //获取数组的个数
        long int count  = [array count];
        NSString *str= [array objectAtIndex:count-1];
        NSLog(@"%@",str);
        
                //遍历数组元素
        for(int i  =  0 ;i <count ;i++)
        {
            NSLog(@"%@",[array objectAtIndex:i]);
        }
                //使用枚举器来遍历数组
        NSEnumerator  * enumerator = [array objectEnumerator];
        
        NSString * obj  = nil;
        while  ( obj = [enumerator nextObject]  )
        {
            NSLog(@"%@",obj);
        }
                //快速枚举
        for (id obj in array) {
            NSLog(@"%@",obj);
        }
       // 如果只是想单纯的输出数组内某种类型的元素
        //把id 改为要输出得类型就ok

可变数组的使用

        NSMutableArray *mutablearray = [NSMutableArray arrayWithCapacity:10];
        [mutablearray addObject:@"test"];
        [mutablearray addObject:@"test"];
        [mutablearray addObject:@"test"];
        //删除一个元素
        [mutablearray removeObject:@"test"];
        [mutablearray removeAllObjects]; //删除全部元素
        //如果用for进行删除,最好要倒着删除 I = length-1;I>=0; I++
        Student *wuKong = [[Student alloc] init];
        [wuKong setName:@"孙悟空"];
        [wuKong setAge:1000];
        
        Student *baJie = [[Student alloc] init];
        [baJie setName:@"八戒"];
        [baJie setAge:800];
        
        Student *shaSeng = [[Student alloc] init];
        [shaSeng setName:@"沙僧"];
        [shaSeng setAge:560];
        
        Student *Boss = [[Student alloc] init];
        [Boss setName:@"唐僧"];
        [Boss setAge:25];
        
        NSMutableArray *WestSkyTeam = [NSMutableArray arrayWithCapacity:10];
        [WestSkyTeam addObject:wuKong];
        [WestSkyTeam addObject:baJie];
        [WestSkyTeam addObject:shaSeng];
        [WestSkyTeam addObject:Boss];
        
        for ( id obj in WestSkyTeam )
        {
            NSLog(@"%@",obj);
        }
        

定义一个排序描述对象

        NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
        //定义一个数组,然后用来盛放排序条件
        NSArray *des = [NSArray arrayWithObject:sd];
        //进行排序
        [WestSkyTeam sortUsingDescriptors:des];
        NSLog(@"==========排序后==========");
        for( id obj in WestSkyTeam)
        {
            NSLog(@"%@",obj);
        }
        NSLog(@"NSArray数组排序后:");
        NSArray *Team2 = [WestSkyTeam sortedArrayUsingDescriptors:des];
        for(id obj in Team2)
        {
            NSLog(@"%@",obj);
        }

 使用自定义方法进行排序

        NSMutableArray * team3 = [NSMutableArray arrayWithCapacity:10];
        [team3 addObject:wuKong];
        [team3 addObject:baJie];
        [team3 addObject: shaSeng];
        [team3 addObject:Boss];
        [team3 sortUsingSelector:@selector(myCompare:)];
        NSLog(@"使用自定义方法排序后:");
        for( id obj in team3)
        {
            NSLog(@"%@",obj);
        }
        
        
        [wuKong release];
        [baJie release];
        [shaSeng release];
        [Boss release];
        

其中如果要使用自定义排序方法就要建一个Student的类,然后建一个自定义的比较方法

Student.h

#import <Foundation/Foundation.h>

@interface Student : NSObject
{
    NSString *name;
    int age ;
}


-(NSComparisonResult)myCompare:(id)obj;

-(void)setName:(NSString *)_name;
-(NSString *)name;

-(void)setAge:(int)_Age;
-(int)age;

-(NSString *)description;
@end

Student.m

#import "Student.h"

@implementation Student

-(NSComparisonResult)myCompare:(id)obj
{
      return [[self name]compare:[obj name]];
}

-(void)setName:(NSString *)_name
{
    name =_name;
}
-(NSString *)name
{
    return name;
}

-(void)setAge:(int)_age
{
    age = _age;
}
-(int)age
{
    return age;
}
-(NSString *)description
{
    return  [NSString  stringWithFormat:@"Name is :%@ Age is :%i",self.name,self.age ];
}
@end

运行后输出结果如下:


2013-08-03 15:34:20.409 集合[367:303] 沙僧
2013-08-03 15:34:20.411 集合[367:303] 孙悟空
2013-08-03 15:34:20.412 集合[367:303] 猪八戒
2013-08-03 15:34:20.412 集合[367:303] 沙僧
2013-08-03 15:34:20.416 集合[367:303] 孙悟空
2013-08-03 15:34:20.417 集合[367:303] 猪八戒
2013-08-03 15:34:20.417 集合[367:303] 沙僧
2013-08-03 15:34:20.417 集合[367:303] 孙悟空
2013-08-03 15:34:20.418 集合[367:303] 猪八戒
2013-08-03 15:34:20.418 集合[367:303] 沙僧
2013-08-03 15:34:20.418 集合[367:303] Name is :孙悟空 Age is :1000
2013-08-03 15:34:20.419 集合[367:303] Name is :八戒 Age is :800
2013-08-03 15:34:20.419 集合[367:303] Name is :沙僧 Age is :560
2013-08-03 15:34:20.420 集合[367:303] Name is :唐僧 Age is :25
2013-08-03 15:34:20.420 集合[367:303] ==========排序后==========
2013-08-03 15:34:20.421 集合[367:303] Name is :唐僧 Age is :25
2013-08-03 15:34:20.421 集合[367:303] Name is :沙僧 Age is :560
2013-08-03 15:34:20.421 集合[367:303] Name is :八戒 Age is :800
2013-08-03 15:34:20.422 集合[367:303] Name is :孙悟空 Age is :1000
2013-08-03 15:34:20.422 集合[367:303] NSArray数组排序后:
2013-08-03 15:34:20.423 集合[367:303] Name is :唐僧 Age is :25
2013-08-03 15:34:20.423 集合[367:303] Name is :沙僧 Age is :560
2013-08-03 15:34:20.424 集合[367:303] Name is :八戒 Age is :800
2013-08-03 15:34:20.424 集合[367:303] Name is :孙悟空 Age is :1000
2013-08-03 15:34:20.425 集合[367:303] 使用自定义方法排序后:
2013-08-03 15:34:20.425 集合[367:303] Name is :八戒 Age is :800
2013-08-03 15:34:20.425 集合[367:303] Name is :唐僧 Age is :25
2013-08-03 15:34:20.426 集合[367:303] Name is :孙悟空 Age is :1000
2013-08-03 15:34:20.426 集合[367:303] Name is :沙僧 Age is :560

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值