《Objective-C编程 第二版》笔记5:NSArray

NSArray是常用的OC类。NSArray实例可以用来保存一组指向其他对象的指针。

首先创建一个新项目DateList.

1、创建数组


#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //创建三个NSDate对象
        NSDate *now = [NSDate date];
        NSDate *tomorrow = [now dateByAddingTimeInterval:24.0*60.0*60.0];
        NSDate *yesterday = [now dateByAddingTimeInterval:-24.0*60.0*60.0];
        
        //创建一个数组包含上述三个对象,注意@符号。NSArray实例包含三个指针,分别指向不同的对象
        NSArray *dateList = @[now, tomorrow, yesterday];
    }
    return 0;
}

备注:
NSArray 的实例无法改变。一旦NSArray 实例被创建后,就无法添加或删除数组里的指针,也无法改变数组的指针顺序。

2、存取数组

可以通过索引存取,索引从0开始。

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //
        NSDate *now = [NSDate date];
        NSDate *tomorrow = [now dateByAddingTimeInterval:24.0*60.0*60.0];
        NSDate *yesterday = [now dateByAddingTimeInterval:-24.0*60.0*60.0];
        
        //
        NSArray *dateList = @[now, tomorrow, yesterday];
        //通过索引输出其中的两个对象
        NSLog(@"The first date is %@",dateList[0]);
        NSLog(@"The third date is %@",dateList[2]);
        //输出包含多少个对象。count消息返回数组中对象个数
        NSLog(@"There are %lu dates.",[dateList count]);
    }
    return 0;
}

3、遍历数组

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //
        NSDate *now = [NSDate date];
        NSDate *tomorrow = [now dateByAddingTimeInterval:24.0*60.0*60.0];
        NSDate *yesterday = [now dateByAddingTimeInterval:-24.0*60.0*60.0];
        
        NSArray *dateList = @[now, tomorrow, yesterday];
        //获取数组中对象个数
        NSUInteger dateCount = [dateList count];
        //遍历数组
        for(int i = 0; i < dateCount; i++ ){
            NSDate *d = dateList[i];
            NSLog(@"Here is a date: %@",d);
        }

        //快速枚举
        for(NSDate *d in dateList){
            NSLog(@"Here is a date:%@",d);
        }
    }
    return 0;
}

4、NSMutableArray

NSMutableArray也是数组,但是它可以添加、删除或对指针进行排序。

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //
        NSDate *now = [NSDate date];
        NSDate *tomorrow = [now dateByAddingTimeInterval:24.0*60.0*60.0];
        NSDate *yesterday = [now dateByAddingTimeInterval:-24.0*60.0*60.0];
        
        NSMutableArray *dateList = [NSMutableArray array];
        //将两个对象加入到数组中
        [dateList addObject:now];
        [dateList addObject:tomorrow];
        //插入到数组的起始位置
        [dateList insertObject:yesterday atIndex:0];
        
        for(NSDate *d in dateList){
            NSLog(@"Here is a date: %@",d);
        }
        
        //删除数组中的yesterday指针
        [dateList removeObjectAtIndex:0];
        NSLog(@"Now the first date is %@",dateList[0]);
    }
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值