码迷,mamicode.com
首页 > 编程语言 > 详细

关于数组内元素是否会被改变的思考

时间:2020-06-13 00:34:42      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:影响   object   思考   count   不同   end   code   系统   打印   

起因

项目中双层for循环时,想当然的认为内部局部指针变量的变化会影响外面数组中模型的地址。结果调试后发现,是不对的。

- (void)pmy_updateCachedArr:(NSArray<HTBNWaitBusesM4Cal *> *)cachedArr     withNewInfoArr:(NSArray<HTBNWaitBusesM4Cal *> *)newInfoArr{
    [newInfoArr enumerateObjectsUsingBlock:^(HTBNWaitBusesM4Cal * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        for (int i = 0; i < cachedArr.count; ++i) {
            HTBNWaitBusesM4Cal *lM = cachedArr[i];
            if ([lM isEqual:obj]) {
                
                //不能这么做,lM地址会变,但是无法影响for循环外面数组中元素的地址!!!思考作用域原理即可理解。
//                lM = obj;

//外面数组中元素的属性是可以被改变的
                lM.gNearestStaM = obj.gNearestStaM;
            }
        }
    }];
}

结论

  • 不同的循环语法不影响结果
  • 数组是否可变不影响结果
  • 想改变数组中模型的地址,必须用系统方法!模型的属性可以被改变
  • 想想变量赋值原理和作用域的概念,可能会豁然开朗

测试代码


#import "ViewController.h"
#import "Person.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self test1];
    [self test2];
    [self test3];
    //[self test4];
    //[self test5];
}

- (void)test1{
    Person *lP1 = [[Person alloc] init];
    lP1.gStr4Name = @"1号";
    
    Person *lP2 = [[Person alloc] init];
    lP2.gStr4Name = @"2号";
    
    lP1 = lP2;
    NSLog(@"%@,%@",lP1,lP2);
    NSLog(@"test1 lP1.gStr4Name:%@",lP1.gStr4Name);
}

- (void)test2{
    Person *lP1 = [[Person alloc] init];
    lP1.gStr4Name = @"1号";
    
    Person *lP2 = [[Person alloc] init];
    lP2.gStr4Name = @"2号";
    
    NSArray *lArr1 = @[lP1];
    NSArray *lArr2 = @[lP2];
    [lArr1 enumerateObjectsUsingBlock:^(Person *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        for (int i = 0; i < lArr2.count; ++i) {
            Person *lP2 = lArr2.lastObject;
            obj = lP2;
        }
    }];
    
    Person *lP = lArr1.firstObject;
    NSLog(@"%@,%@",lP1,lP2);
    
    //原以为会打印 2号,想想变量赋值原理和作用域的概念,豁然开朗
    NSLog(@"test2 lP.gStr4Name:%@",lP.gStr4Name);
}

- (void)test3{
    Person *lP1 = [[Person alloc] init];
    lP1.gStr4Name = @"1号";
     NSLog(@"1  P1:%@",lP1);
    Person *lP2 = [[Person alloc] init];
    lP2.gStr4Name = @"2号";
     NSLog(@"1  P2:%@",lP2);
    
    NSArray *lArr1 = @[lP1];
    NSArray *lArr2 = @[lP2];
    
    //不同的循环语法不影响结果
    for (int i = 0; i < lArr1.count; ++i) {
        Person *lP1 = lArr1.firstObject;
         NSLog(@"2  P1:%@",lP1);
        for (int j = 0; j < lArr2.count; ++j) {
            Person *lP2 = lArr2.lastObject;
            lP1 = lP2;
            NSLog(@"2 P2:%@",lP2);
        }
         NSLog(@"3  P1:%@",lP1);
    }
    
    Person *lP = lArr1.firstObject;
    NSLog(@"4:%@,%@",lP1,lP2);
    NSLog(@"test3 lP.gStr4Name:%@",lP.gStr4Name);
}

- (void)test4{
    Person *lP1 = [[Person alloc] init];
    lP1.gStr4Name = @"1号";
     NSLog(@"1  P1:%@",lP1);
    Person *lP2 = [[Person alloc] init];
    lP2.gStr4Name = @"2号";
     NSLog(@"1  P2:%@",lP2);
    
    //数组是否可变不影响结果
    NSMutableArray *lArr1 = [NSMutableArray arrayWithArray:@[lP1]];
    NSMutableArray *lArr2 = [NSMutableArray arrayWithArray:@[lP2]];
    for (int i = 0; i < lArr1.count; ++i) {
        Person *lP1 = lArr1.firstObject;
         NSLog(@"2  P1:%@",lP1);
        for (int j = 0; j < lArr2.count; ++j) {
            Person *lP2 = lArr2.lastObject;
            lP1 = lP2;
            NSLog(@"2 P2:%@",lP2);
        }
         NSLog(@"3  P1:%@",lP1);
    }
    
    Person *lP = lArr1.firstObject;
    NSLog(@"4:%@,%@",lP1,lP2);
    NSLog(@"test3 lP.gStr4Name:%@",lP.gStr4Name);
}

- (void)test5{
    Person *lP1 = [[Person alloc] init];
    lP1.gStr4Name = @"1号";
     NSLog(@"1  P1:%@",lP1);
    Person *lP2 = [[Person alloc] init];
    lP2.gStr4Name = @"2号";
     NSLog(@"1  P2:%@",lP2);
    
    NSMutableArray *lArr1 = [NSMutableArray arrayWithArray:@[lP1]];
    NSMutableArray *lArr2 = [NSMutableArray arrayWithArray:@[lP2]];
    for (int i = 0; i < lArr1.count; ++i) {
        Person *lP1 = lArr1.firstObject;
         NSLog(@"2  P1:%@",lP1);
        for (int j = 0; j < lArr2.count; ++j) {
            Person *lP2 = lArr2.lastObject;
            
            //想改变数组内容,必须用系统方法!
            [lArr1 replaceObjectAtIndex:0 withObject:lP2];
        }
         NSLog(@"3  P1:%@",lP1);
    }
    
    Person *lP = lArr1.firstObject;
    NSLog(@"4:%@,%@",lP1,lP2);
    NSLog(@"test3 lP.gStr4Name:%@",lP.gStr4Name);
}


@end

关于数组内元素是否会被改变的思考

标签:影响   object   思考   count   不同   end   code   系统   打印   

原文地址:https://www.cnblogs.com/Dast1/p/13094218.html

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