每日一练:OC中的NSarray和NSMutableArray

本文介绍了一个使用Objective-C进行数组操作的例子,包括创建不同类型的数组、遍历数组元素、添加和移除对象等基本操作。

#import <Foundation/Foundation.h>


int main(int argc, const char * argv[]) {

    @autoreleasepool {

        // insert code here...


        //Array A

        NSArray *myArraya = @[@"China",@"USA",@"France"];

        NSLog(@"The Array A is %@", myArraya);

        

        //Array B

        NSDate *datea = [NSDate date];

        sleep(2);

        NSDate *dateb = [NSDate date];

        sleep(2);

        NSDate *datec = [NSDate date];

        

        NSArray *myArrayb = @[datea, dateb, datec];

        NSLog(@"The Array B is %@", myArrayb);

        

        NSLog(@"The first value of array b is %@", myArrayb[0]);

        NSLog(@"The third value of array b is %@", myArrayb[2]);

        

        NSLog(@"The count num of array b is %lu", [myArrayb count]);

        

        //*遍历数组的值

        

        for(int i = 0; i < [myArrayb count]; i++){

            NSDate *d = myArrayb[i];

            NSLog(@"The date is %@", d);

        }

        

        

        //*遍历数组的值-方法2

        

        for(NSDate *dd in myArrayb){

            NSLog(@"The second date is %@", dd);

        }

        

        //数组3 NSMutableArray

        

        NSDate *today = [NSDate date];

        NSDate *yesterday = [today dateByAddingTimeInterval:-24*60*60];

        NSDate *tomorrow = [today dateByAddingTimeInterval:24*60*60];

        

        NSMutableArray *myArrayC = [NSMutableArray array];

        [myArrayC addObject:today];

        [myArrayC addObject:tomorrow];

        

        NSLog(@"My Array C is : %@", myArrayC);

        

        [myArrayC insertObject:yesterday

                       atIndex:0];

        

        for(NSDate *dc in myArrayC){

            NSLog(@"The Array C is : %@",dc);

        }

        

        [myArrayC removeObjectAtIndex:0];

        

        NSLog(@"The Array C has updated to : %@", myArrayC);

    }

    return 0;

}


Result:

2018-03-11 00:36:18.490239+0800 TOCNSArraya[62051:14140071] The Array A is (

    China,

    USA,

    France

)

2018-03-11 00:36:22.503433+0800 TOCNSArraya[62051:14140071] The Array B is (

    "2018-03-10 16:36:18 +0000",

    "2018-03-10 16:36:20 +0000",

    "2018-03-10 16:36:22 +0000"

)

2018-03-11 00:36:22.503532+0800 TOCNSArraya[62051:14140071] The first value of array b is Sun Mar 11 00:36:18 2018

2018-03-11 00:36:22.503583+0800 TOCNSArraya[62051:14140071] The third value of array b is Sun Mar 11 00:36:22 2018

2018-03-11 00:36:22.503603+0800 TOCNSArraya[62051:14140071] The count num of array b is 3

2018-03-11 00:36:22.503689+0800 TOCNSArraya[62051:14140071] The date is Sun Mar 11 00:36:18 2018

2018-03-11 00:36:22.503736+0800 TOCNSArraya[62051:14140071] The date is Sun Mar 11 00:36:20 2018

2018-03-11 00:36:22.503760+0800 TOCNSArraya[62051:14140071] The date is Sun Mar 11 00:36:22 2018

2018-03-11 00:36:22.503798+0800 TOCNSArraya[62051:14140071] The second date is Sun Mar 11 00:36:18 2018

2018-03-11 00:36:22.503811+0800 TOCNSArraya[62051:14140071] The second date is Sun Mar 11 00:36:20 2018

2018-03-11 00:36:22.503823+0800 TOCNSArraya[62051:14140071] The second date is Sun Mar 11 00:36:22 2018

2018-03-11 00:36:22.504032+0800 TOCNSArraya[62051:14140071] My Array C is : (

    "2018-03-10 16:36:22 +0000",

    "2018-03-11 16:36:22 +0000"

)

2018-03-11 00:36:22.504102+0800 TOCNSArraya[62051:14140071] The Array C is : Sat Mar 10 00:36:22 2018

2018-03-11 00:36:22.504122+0800 TOCNSArraya[62051:14140071] The Array C is : Sun Mar 11 00:36:22 2018

2018-03-11 00:36:22.504180+0800 TOCNSArraya[62051:14140071] The Array C is : Mon Mar 12 00:36:22 2018

2018-03-11 00:36:22.504330+0800 TOCNSArraya[62051:14140071] The Array C has updated to : (

    "2018-03-10 16:36:22 +0000",

    "2018-03-11 16:36:22 +0000"

)

Program ended with exit code: 0



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值