Objective-C NSArray 基础 (NSString NSNumber NSEnumerator )

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        //数组用来存储对象 
        //创建 NSNumber 对象,作为 NSArray添加对象
        NSNumber *Num1=[[NSNumber alloc] initWithInt:1];
        NSNumber *Num2=[[NSNumber alloc] initWithInt:2];
        NSNumber *Num3=[[NSNumber alloc] initWithInt:3];
        //1.创建数组 NSArray

        NSArray *Arr1=[NSArray arrayWithObjects:@"one", @"two",@"three",@"four",@"five",nil];
        NSLog(@"%@",Arr1);
        
        //输出
        /*
         (
         one,
         two,
         three,
         four,
         five
         )
         */

        NSArray *Arr2=[[NSArray alloc ] initWithObjects:Num1,Num2,Num3,@"two", nil];
        NSLog(@"%@",Arr2);
        //输出
        /*
        (
         1,
         2,
         3
         )
         */
        
        //2.获取数组中指定元素
        NSLog(@"%@", [Arr1 objectAtIndex:1]);//输出 two
        NSLog(@"%@",[Arr1 objectAtIndexedSubscript:2]);//以下标索引  输出 three
        
        //3.获取元素个数
        NSLog(@"%li",[Arr1 count]);//输出 5
        
        //4.判断object是否在数组中 返回BOOL类型 示例
        BOOL Bool1=[Arr1 containsObject:@"one"];
        NSLog(@"%i",Bool1);
        
        //5.将数组元素以“YY”分割合并 实现从数组到字符串
        NSString *Str=[Arr1 componentsJoinedByString:@"YY"];
        NSLog(@"%@",Str);//输出 oneYYtwoYYthreeYYfourYYfive
        //实现从字符串到数组
        NSString *String=@"one,two,three,four";
        NSArray *Arra=[String componentsSeparatedByString:@","];
        NSLog(@"%@",Arra);
        //输出
        /*
         (
         one,
         two,
         three,
         four
         )
         */
        
        //6.判断两个数组第一个相同的元素
        NSString *Str2=[Arr1 firstObjectCommonWithArray:Arr2];
        NSLog(@"%@",Str2); //输出 two
        
        //7.从一个数组得到元素
        NSArray *Arr3=[NSArray arrayWithArray:Arr1];
        NSLog(@"%@",Arr3);
        //输出
        /*
         (
         one,
         two,
         three,
         four,
         five
         )
         */
        //8.从 const id [] 获取 object  该方法要注意使用 易错
        NSString *pStr[5];
        pStr[0]=@"ff";
        pStr[1]=@"one";
        pStr[2]=@"two";
        NSArray *Arr4=[NSArray arrayWithObjects:pStr count:3];
        NSLog(@"%@",Arr4);
        //输出
        /*
         (
         ff,
         one,
         two
         )
         */
        
        //9.在一定范围查找object位置
        NSUInteger nNum=[Arr1 indexOfObject:@"three" inRange:NSMakeRange(1, 3)];
        NSLog(@"%lu",(unsigned long)nNum);//2
        
        //10创建可变动态性数组 NSMutableArray
        NSMutableArray *MuArr=[NSMutableArray arrayWithCapacity:20];
        MuArr=[NSMutableArray arrayWithArray:Arr1];
        
        //11.快速枚举输出
        for (id obj in MuArr) {
            NSLog(@"枚举输出 %@",obj);//会一个一个输出
        }
        
        
        //12.NSMutableArray 是 NSArray 子类 有多出一些方法 如 添加 替换 插入等
        
        //替换
        [MuArr replaceObjectAtIndex:1 withObject:@"off"];
        NSLog(@"%@",MuArr);
        //输出
        /*
         (
         one,
         off,
         three,
         four,
         five
         )
         */
        
        //13 .移除部分  移除全部等
        [MuArr removeAllObjects];
        NSLog(@"%li",[MuArr count]);//0
        
        //14.添加对象
        [MuArr addObject:@"one"];
        [MuArr addObject:@"two"];
        NSLog(@"%@",MuArr);
        
        //15.将结构体添加至数组中 (NSRange NSSize NSRect NSPoint等)
        NSRange range=NSMakeRange(3, 4);
        [MuArr addObject:[NSValue valueWithRange:range]];
        NSRect rect=NSMakeRect(1, 1, 10, 20);
        [MuArr addObject:[NSValue valueWithRect:rect]];
        NSLog(@"%@",MuArr);
        //输出
        /*
        (
         one,
         two,
         "NSRange: {3, 4}",
         "NSRect: {{1, 1}, {10, 20}}"
         )
         */
        
        //16.用枚举器 NSEnumerator 浏览
        NSEnumerator *Enumerator1=[MuArr objectEnumerator];
        id object1=nil;
        while (object1=[Enumerator1 nextObject]) {
            NSLog(@"%@",object1);
        }
        
        //17.用枚举器 NSEnumerator 从后向前浏览、
        //创建 NSEnumerator
        NSEnumerator *Enumerator2=[MuArr reverseObjectEnumerator];
        id object2=nil;
        while (object2=[Enumerator2 nextObject]) {
            NSLog(@"%@",object2);
        }
        //输出
        /*
         NSRect: {{1, 1}, {10, 20}}
         NSRange: {3, 4}
         two
         one
         */
                   
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值