码迷,mamicode.com
首页 > 其他好文 > 详细

NSArray

时间:2015-07-19 23:16:14      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:

    /********NSArray**********/

    

    NSObject *obj = [NSObject new];

    NSObject *obj1 = [NSObject new];

    Car *car = [Car new];

    

    // 1.创建数组

    NSArray *arr1 = [NSArray arrayWithObjects:@"帅锋",@"xingchen",@"honghai",@"c",obj,car,nil];// 使用arrayWithObjects:来创建数组的时候需要以nil结尾,代表数组结束,所以数组中不能存放nil,另外nil并不是数组的元素

    //NSArray *arr1 = @[@"张三",@"xingchen",@"honghai",@"fusong",obj,car]; // NSArray中只能存放对象,并且对对象的类别没有统一要求

    NSLog(@"arr1 >>>> %@", arr1);

    

    

    // 2.访问数组

    //arr1[4] = obj1; NSArray是不可变数组,不能更改数组中的元素

    

    NSLog(@"arr1[4] >>>> %@", arr1[4]);

    NSLog(@"arr1[5] >>>> %@", [arr1 objectAtIndex:0]);

    

    // 3.长度

    NSLog(@"arr1‘length >>> %ld", [arr1     count]);

    

    // 4.是否包含对象  contains 包含容纳

    if ([arr1 containsObject:obj1]) {

        NSLog(@"YES");

    } else {

        NSLog(@"NO");

    }

    

    // 5.遍历数组的方式

    // 5.1 for循环

    

    // 5.2 枚举器

    NSArray *arr2 = @[@"junkao",@"chunyang"];

    NSEnumerator *enumrator = [arr2 objectEnumerator];

    id object = [enumrator nextObject];

    object = [enumrator nextObject];

    object = [enumrator nextObject];

    NSLog(@"object >>> %@", object);

    NSLog(@"object >>> %@", arr1[0]);

 

    // 5.3 快速枚举

    // 快速枚举语法:for(对象类型 中间变量 in 需要遍历的数组)

    for (id arrObj in arr1) {

        NSLog(@"arrobj >>>> %@ <<<<", arrObj);

    }

    

    Car *car1 = [Car new];

    Car *car2 = [Car new];

    Car *car3 = [Car new];

    NSArray *arr3 = @[car1, car2, car3];

    for (Car *car in arr3) {

        NSLog(@"car >>>> %@", car);

    }

    

    // 6.数组连接

    NSArray *arr4 = [arr1 arrayByAddingObject:@"yuanao"];

    NSArray *arr5 = [arr1 arrayByAddingObjectsFromArray:arr2];

    NSLog(@"arr1 >>> %@", arr1);

    NSLog(@"arr2 >>> %@", arr2);

    NSLog(@"arr4 >>> %@", arr4);

    NSLog(@"arr5 >>> %@", arr5);

 

    // 7.字符串分割

    NSString *names = @"guoyu,zhangting,gejing,chaoya";

    NSArray *nameArr = [names componentsSeparatedByString:@","];//对字符串发消息componentsSeparatedByString 用‘,’分隔成数组

    NSLog(@"nameArr >>> %@", nameArr);

    NSString *names2 = [nameArr componentsJoinedByString:@"-"];//数组变成字符串

    NSLog(@"names2 >>> %@", names2);

    

    

    /**********NSMutableArray***********/

    NSMutableArray *mtArr = [NSMutableArray arrayWithArray:@[@"dongdong",@"王帅"]];//arrayWithArray  用一个不可变数组变成一个可变数组  stringWithString 用一个不可变字符串变成一个可变字符串

    

    mtArr[0] = @"qilin";

    NSLog(@"mtArr >>> %@", mtArr);

    NSLog(@"mtArr >>>> %@", [mtArr objectAtIndex:1]);

    [mtArr addObject:@"litao"];

    NSLog(@"mtArr >>> %@", mtArr);

    [mtArr addObjectsFromArray:@[@"awfeawfaw",@"aefwfawfewf"]];

    [mtArr removeObject:@"qilin"];

    NSLog(@"mtArr >>> %@", mtArr);

    

    // 指定范围删除

    //[mtArr removeObject:@"qilin" inRange:<#(NSRange)#>];

    // 在指定位置插入新元素

    //[mtArr insertObject:<#(id)#> atIndex:<#(NSUInteger)#>]

    // 替换

    //[mtArr replaceObjectAtIndex:<#(NSUInteger)#> withObject:<#(id)#>]

 

 

2015-07-19 20:50:14.339 testNSArray[12353:1834956] arr1 >>>> (

    "\U5e05\U950b",

    xingchen,

    honghai,

    c,

    "<NSObject: 0x10030a030>",

    "\U6211\U662f\U4e00\U8f86\U8f66"

)

2015-07-19 20:50:14.340 testNSArray[12353:1834956] arr1[4] >>>> <NSObject: 0x10030a030>

2015-07-19 20:50:16.921 testNSArray[12353:1834956] arr1[5] >>>> 张三

2015-07-19 20:50:16.922 testNSArray[12353:1834956] arr1‘length >>> 6

2015-07-19 20:50:16.922 testNSArray[12353:1834956] NO

2015-07-19 20:50:16.925 testNSArray[12353:1834956] object >>> (null)

2015-07-19 20:50:16.925 testNSArray[12353:1834956] object >>> 张三

2015-07-19 20:50:21.880 testNSArray[12353:1834956] arrobj >>>> 张三 <<<<

2015-07-19 20:50:23.131 testNSArray[12353:1834956] arrobj >>>> xingchen <<<<

2015-07-19 20:50:24.416 testNSArray[12353:1834956] arrobj >>>> honghai <<<<

2015-07-19 20:50:25.617 testNSArray[12353:1834956] arrobj >>>> c <<<<

2015-07-19 20:50:27.186 testNSArray[12353:1834956] arrobj >>>> <NSObject: 0x10030a030> <<<<

2015-07-19 20:50:30.168 testNSArray[12353:1834956] arrobj >>>> 我是一辆车 <<<<

2015-07-19 20:50:30.169 testNSArray[12353:1834956] car >>>> 我是一辆车

2015-07-19 20:50:30.169 testNSArray[12353:1834956] car >>>> 我是一辆车

2015-07-19 20:50:30.169 testNSArray[12353:1834956] car >>>> 我是一辆车

2015-07-19 20:50:30.169 testNSArray[12353:1834956] arr1 >>> (

    "\U5e05\U950b",

    xingchen,

    honghai,

    c,

    "<NSObject: 0x10030a030>",

    "\U6211\U662f\U4e00\U8f86\U8f66"

)

2015-07-19 20:50:30.169 testNSArray[12353:1834956] arr2 >>> (

    junkao,

    chunyang

)

2015-07-19 20:50:30.169 testNSArray[12353:1834956] arr4 >>> (

    "\U5e05\U950b",

    xingchen,

    honghai,

    c,

    "<NSObject: 0x10030a030>",

    "\U6211\U662f\U4e00\U8f86\U8f66",

    yuanao

)

2015-07-19 20:50:30.169 testNSArray[12353:1834956] arr5 >>> (

    "\U5e05\U950b",

    xingchen,

    honghai,

    c,

    "<NSObject: 0x10030a030>",

    "\U6211\U662f\U4e00\U8f86\U8f66",

    junkao,

    chunyang

)

2015-07-19 20:50:30.169 testNSArray[12353:1834956] nameArr >>> (

    guoyu,

    zhangting,

    gejing,

    chaoya

)

2015-07-19 20:50:30.170 testNSArray[12353:1834956] names2 >>> guoyu-zhangting-gejing-chaoya

2015-07-19 21:54:45.381 testNSArray[12353:1834956] mtArr >>> (

    qilin,

    "\U738b\U5e05"

)

2015-07-19 21:54:45.382 testNSArray[12353:1834956] mtArr >>>> 王帅

2015-07-19 21:54:45.382 testNSArray[12353:1834956] mtArr >>> (

    qilin,

    "\U738b\U5e05",

    litao

)

2015-07-19 21:54:45.382 testNSArray[12353:1834956] mtArr >>> (

    "\U738b\U5e05",

    litao,

    awfeawfaw,

    aefwfawfewf

)

NSArray

标签:

原文地址:http://www.cnblogs.com/10-19-92/p/4659932.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!