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

NSArray和NSMutableArray

时间:2014-11-26 16:14:56      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:style   blog   ar   color   sp   div   log   bs   ad   

//1. NSArray
    
    EOItems *eOItems = [[EOItems alloc] init];
    eOItems.ID = [NSNumber numberWithInt:1];
    NSArray *arr = [NSArray arrayWithObjects:eOItems, nil];
    eOItems.ID = [NSNumber numberWithInt:2];
    NSLog(@"eOItems.ID =%@, ((EOItems *)[arr objectAtIndex:0]).ID=%@", eOItems.ID, ((EOItems *)[arr objectAtIndex:0]).ID);
    //打印结果: eOItems.ID =2, ((EOItems *)[arr objectAtIndex:0]).ID=2
    
    EOItems *eOItems2 = [arr objectAtIndex:0];
    eOItems2.ID = [NSNumber numberWithInt:22];
    NSLog(@"eOItems2 =%@, ((EOItems *)[arr objectAtIndex:0]).ID=%@", eOItems2.ID, ((EOItems *)[arr objectAtIndex:0]).ID);
    //打印结果: eOItems2 =22, ((EOItems *)[arr objectAtIndex:0]).ID=22
    
    NSLog(@"eOItems.ID =%@", eOItems.ID);
    //打印结果:eOItems.ID =22
    
    //由此可见: NSArray只是存储对象的索引并没有真正复制对象, 对象由始至终只有一个
    
    //2.NSMutableArray
    
    NSMutableArray *mutableArray = [arr mutableCopy];
    ((EOItems *)[mutableArray objectAtIndex:0]).ID = [NSNumber numberWithInt:333];
    NSLog(@"eOItems.ID=%@", eOItems.ID);
    NSLog(@"eOItems2.ID=%@", eOItems2.ID);
    NSLog(@"((EOItems *)[mutableArray objectAtIndex:0]).ID=%@", ((EOItems *)[mutableArray objectAtIndex:0]).ID);
    //输出结果
    //eOItems.ID=333
    //eOItems2.ID=333
    //((EOItems *)[mutableArray objectAtIndex:0]).ID=333
    
    //经NSArray mutableCopy得来的 NSMutableArray, 对象也始终只有一个
    
    NSMutableArray *mulArr = [NSMutableArray array ];
    EOItems *mulItems = [[EOItems alloc] init];
    mulItems.ID = [NSNumber numberWithInt:111];
    [mulArr addObject:mulItems];
    ((EOItems *)[mulArr objectAtIndex:0]).ID = [NSNumber numberWithInt:222];
    NSLog(@"mulItems.ID=%@, ((EOItems *)[mulArr objectAtIndex:0]).ID=%@",mulItems.ID, ((EOItems *)[mulArr objectAtIndex:0]).ID);
    //输出结果
    //mulItems.ID=222, ((EOItems *)[mulArr objectAtIndex:0]).ID=222
    
    mulItems.ID = [NSNumber numberWithInt:333333];
    NSLog(@"mulItems.ID=%@, ((EOItems *)[mulArr objectAtIndex:0]).ID=%@",mulItems.ID, ((EOItems *)[mulArr objectAtIndex:0]).ID);
    //输出结果
    //mulItems.ID=333333, ((EOItems *)[mulArr objectAtIndex:0]).ID=333333

总结: NSArray和NSMutableArray区别在于后者可变可以添加,删除,修改

他们存储的都是对象的引用

 

EOItems *item1 = [[EOItems alloc] init];
    item1.ID = [NSNumber numberWithInt:1];
    
    EOItems *item2 = [[EOItems alloc] init];
    item2.ID = [NSNumber numberWithInt:2];
    
    EOItems *item3 = item1;
    NSLog(@"item3.ID=%@", item3.ID);
    
    item3 = item2;
    NSLog(@"item3.ID=%@", item3.ID);

 

NSArray和NSMutableArray

标签:style   blog   ar   color   sp   div   log   bs   ad   

原文地址:http://www.cnblogs.com/apem/p/4123357.html

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