Foundation框架 - NSArray类 、NSMutableArray类

NSArray类 、NSMutableArray类

数组创建

 NSLog(@"*************************** 数组创建 ****************************");

        //方式一:
        NSArray *arr1 = [NSArray arrayWithObject:@"jack"];
        NSLog(@"arr1数组元素为:%@",arr1);

        // nil是数组元素结束的标记
        NSArray *arr2 = [NSArray arrayWithObjects:@"tom",@"jack", @"rose", nil];
        NSLog(@"arr1数组的长度:%ld",arr2.count);

        //方式二:快速创建一个NSArray对象,推荐使用
        NSArray *arr3 = @[@"jack", @"rose", @"tom",@"lily",@"bob"];
        NSLog(@"arr3数组的长度:%ld",arr3.count);

        //方式三:构建一个C数组类型
        NSNumber* pnumber[4]={[NSNumber numberWithInt:2],[NSNumber numberWithInt:2],
        [NSNumber numberWithDouble:3.4f],[NSNumber numberWithChar:'a']};
        NSArray*  arr4=[NSArray arrayWithObjects:pnumber count:4];
        NSLog(@"arr4数组的长度:%ld",arr4.count);

数组拷贝

        NSLog(@"*************************** 数组拷贝 ****************************");


       //拷贝数组,原数组必须也是NSArray类型,浅拷贝:只拷贝指针地址
       //+ (instancetype)arrayWithArray:(NSArray *)anArray
        NSArray* copyArr=[NSArray arrayWithArray:arr4];
        NSLog(@"拷贝后的数组copyArr:%@",copyArr);

数组遍历


        NSLog(@"*************************** 数组遍历 ****************************");


        //方式一:
        for (int i = 0; i<arr3.count; i++)
        {
            NSLog(@"方式一遍历结果:%@", arr3[i]);
        }

        //方式二:   id obj代表着数组中的每一个元素
        for (id obj in arr3)
        {
            NSUInteger i = [arr3 indexOfObject:obj];
            NSLog(@"方式二遍历结果:%ld --%@", i, obj);
        }

        //方式三:每遍历到一个元素,就会调用一次block,并且当前元素和索引位置当做参数传给block
        [arr3 enumerateObjectsUsingBlock:

         ^(id obj, NSUInteger idx, BOOL *stop)
         {
             NSLog(@"方式三遍历结果:%ld ——— %@", idx, obj);

         }];

        //方式四:(NSEnumerator快速迭代) - (NSEnumerator *)objectEnumerator
        NSEnumerator* nse=arr3.objectEnumerator;
        NSNumber* item=nil;
        int j=0;
        while((item=nse.nextObject)!=nil)
        {
            NSLog(@"方式四遍历结果:nse[%d]:%@",j++,item);
        }

反向遍历

        NSLog(@"***************************** 反向遍历 *****************************");
        //获取一个反向迭代器(枚举器)
        //- (NSEnumerator *)reverseObjectEnumerator
        NSEnumerator* emu =[arr3 reverseObjectEnumerator];
        NSString* item1=nil;
        while ((item1=emu.nextObject)) {
            NSLog(@"%@",item1);
        }

数组比较

        NSLog(@"***************************** 数组比较 *****************************");

        NSString* str1=[NSString stringWithFormat:@"aabb"];
        NSString* str2=[NSString stringWithFormat:@"bbcc"];
        NSString* str3=@"aabb";
        NSLog(@"str1_address:%p",str1);
        NSLog(@"str3_address:%p",str3);


        NSArray* arr=[NSArray arrayWithObjects:str1,str2,str3, nil];
        NSInteger index =[arr indexOfObject:str3];
        NSLog(@"str3 index:%ld",index);

        //使用identicalTo比较,用对象的地址比较
        // - (NSUInteger)indexOfObjectIdenticalTo:(id)anObject
        NSInteger index2=[arr indexOfObjectIdenticalTo:str3];
        NSLog(@"str3 index2:%ld",index2);

动态数组

#import <Foundation/Foundation.h>

@interface Student : NSObject

@property NSString* name;
@property NSString* stid;
@property NSString* schoolName;

- (void)printMe;

+ (instancetype)studentWithData:(NSString *)name withStid:(NSString *)stid withSchoolName:(NSString *)schoolName;
@end
#import "Student.h"
@implementation Student
@synthesize name,stid,schoolName;

- (void)printMe
{
    NSLog(@"学号:%@,姓名:%@,学校:%@",stid,name,schoolName); //访问成员变量,效率高
    //NSLog(@"学号:%@,姓名:%@,学校:%@",[self stid],[self name],[self schoolName]); //访问属性
}

+ (instancetype)studentWithData:(NSString*)name withStid:(NSString*)stid withSchoolName:(NSString*)schoolName
{
    Student* stu=[self alloc];
    stu.name=name;
    stu.stid=stid;
    stu.schoolName=schoolName;
    return stu;
}
@end
NSLog(@"***************************** 动态数组 *****************************");

        //给数组中的每个元素都发起一个方法的调用消息(OC的动态性绝佳体现)
        //- (void)makeObjectsPerformSelector:(SEL)aSelector
        Student* stu1 =[Student studentWithData:@"王小明" withStid:@"001"
                                 withSchoolName:@"东大"];
        Student* stu2 =[Student studentWithData:@"孙大宝" withStid:@"002"
                                 withSchoolName:@"北大"];
        Student* stu3 =[Student studentWithData:@"李小琳" withStid:@"003"
                                 withSchoolName:@"南大"];
        NSArray* stuArray=[NSArray arrayWithObjects:stu1,stu2,stu3, nil];

        //通过编译器指令,查询得到一个printMe的方法类型
        SEL printme=@selector(printMe);

        //- (void)makeObjectsPerformSelector:(SEL)aSelector
        [stuArray makeObjectsPerformSelector:printme];
        [stuArray makeObjectsPerformSelector:@selector(setSchoolName:) withObject:@"网博"];

         NSLog(@"输出改变学校后的元素");
        [stuArray makeObjectsPerformSelector:printme];

可变数组

NSLog(@"***************************** 可变数组 *****************************");


        NSMutableArray* mtbArr=[NSMutableArray arrayWithCapacity:10];
        //产生一个空对象
        [mtbArr addObject:[NSNull alloc]];
        [mtbArr addObject:[NSString stringWithUTF8String:"明天就是2015年了"]];
        NSArray* arr5=[NSArray arrayWithObjects:
                    @"今",@"天",@"是",@"2014",@"年",@"的",@"最",@"后",@"一",@"天",nil];
        [mtbArr addObjectsFromArray:arr5];
        NSLog(@"数组的最后长度是:%ld",mtbArr.count);

        //插入元素
        [mtbArr insertObject:@"$$$" atIndex:3];
        int i=0;
        for (id item in mtbArr) {
            NSLog(@"item[%d]:%@",i++,item);
        }

        //删除元素
        [mtbArr removeObject:@"$$$"];
        [mtbArr removeObjectAtIndex:mtbArr.count-2];
        i=0;//reset i
        for (id item in mtbArr) {
            NSLog(@"item[%d]:%@",i++,item);
        }

        //删除多个元素
        for (int i=0; i<mtbArr.count; i++) {
            id item=[mtbArr objectAtIndex:i];
            if ([item isEqual:@"天"]) {
                [mtbArr removeObjectAtIndex:i];
                NSLog(@"删除成功!");
            }
        }

重点内容

#import <Foundation/Foundation.h>

@interface StudentScore : NSObject
@property(nonatomic,assign) NSUInteger stuID;
@property(nonatomic,strong) NSString* name;
@property(nonatomic,assign) double score;


+ (instancetype)studentWithScore:(NSUInteger)_stuID withName:(NSString*)_name withScore:(double) _score;

- (NSComparisonResult)sortCustom:(id)next;
@end
#import "StudentScore.h"

@implementation StudentScore
@synthesize stuID,name,score;

-(NSString *)description
{
    NSString* str=[NSString stringWithFormat:@"学号:%lu,姓名:%@,成绩:%f",stuID,name,score];
    return str;
}

+(instancetype)studentWithScore:(NSUInteger)_stuID withName:(NSString *)_name withScore:(double)_score
{
    StudentScore* instance =[StudentScore alloc];
    instance.stuID=_stuID;
    instance.name=_name;
    instance.score=_score;
    return instance;
}

- (NSComparisonResult)sortCustom:(id)next
{
    if (score<[next score]) {
        return NSOrderedAscending;
    }else if(score==[next score]){
        return NSOrderedSame;
    }else{
        return NSOrderedDescending;
    }
}

//KVC协议 用key来查找Value(暂时不管)
-(id)valueForKey:(NSString *)key
{
    if([key isEqualToString:@"name"])
    {
        return name;
    }
    if([key isEqualToString:@"stuID"])
    {
        return [NSNumber numberWithUnsignedInteger:stuID];
    }
    if([key isEqualToString:@"score"])
    {
        return [NSNumber numberWithDouble:score];
    }
    return nil;
}

//KVC协议 用key/Value来设置属性(暂时不管)
-(void)setValue:(id)value forKey:(NSString *)key
{
    if([key isEqualToString:@"name"])
    {
        name = value;
    }
    if([key isEqualToString:@"stuID"])
    {
        stuID = [value unsignedIntegerValue];
    }
    if([key isEqualToString:@"score"])
    {
        score = [value doubleValue];
    }

}

@end
        NSLog(@"***************************** 数组排序 *****************************");

        NSMutableArray* scoreList=[NSMutableArray arrayWithCapacity:5];
        for (int i=0; i<5; i++) {
            StudentScore* stuScore=[StudentScore studentWithScore:200+i
             withName:[NSString stringWithFormat:@"Robot[%d]",i] withScore:0.0f];
            [scoreList addObject:stuScore];
        }
        //自定义StudentScore文件
        ((StudentScore *)scoreList[0]).score=60.5f;
        ((StudentScore *)scoreList[1]).score=70.5f;
        ((StudentScore *)scoreList[2]).score=90.2f;
        ((StudentScore *)scoreList[3]).score=65.5f;
        ((StudentScore *)scoreList[4]).score=80.5f;

        [scoreList sortUsingSelector:@selector(sortCustom:)];
        int n=0;
        for (StudentScore* score in scoreList) {
            NSLog(@"score[%d]:%@",n++,score);
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值