美文网首页
iOS NSMutableArray和NSArray实例执行mu

iOS NSMutableArray和NSArray实例执行mu

作者: 赵哥窟 | 来源:发表于2020-06-19 16:02 被阅读0次
        NSMutableArray *arrM = [NSMutableArray arrayWithArray:@[@1,@2]];
    
        id cp1 = [arrM mutableCopy];
        id cp2 = [arrM copy];
    
        NSLog(@"arrM %p, class %@", arrM, NSStringFromClass([arrM class]));
        NSLog(@"cp1  %p, class %@", cp1, NSStringFromClass([cp1 class]));
        NSLog(@"cp2  %p, class %@", cp2, NSStringFromClass([cp2 class]));
        
        NSArray *arrI = @[@1,@2];
    
        id cp3 = [arrI mutableCopy];
        id cp4 = [arrI copy];
    
        NSLog(@"arrI %p, class %@", arrI, NSStringFromClass([arrI class]));
        NSLog(@"cp3  %p, class %@", cp3, NSStringFromClass([cp3 class]));
        NSLog(@"cp4  %p, class %@", cp4, NSStringFromClass([cp4 class]));
    
    2020-06-19 15:59:06.153599+0800 Test[81342:2338254] arrM 0x600000c73090, class __NSArrayM
    2020-06-19 15:59:06.153669+0800 Test[81342:2338254] cp1  0x600000c73030, class __NSArrayM
    2020-06-19 15:59:06.153701+0800 Test[81342:2338254] cp2  0x600000245660, class __NSArrayI
    2020-06-19 15:59:06.153739+0800 Test[81342:2338254] arrI 0x600000245680, class __NSArrayI
    2020-06-19 15:59:06.153773+0800 Test[81342:2338254] cp3  0x600000c72fd0, class __NSArrayM
    2020-06-19 15:59:06.153809+0800 Test[81342:2338254] cp4  0x600000245680, class __NSArrayI
    

    上面的问题分两种情况:

    • 当可变对象mutablecopy和copy之后,都会生成新的对象,只是新的对象的类型一个是可变的一个是不可变的。
    • 当不可变对象mutablecopy和copy之后,只有mutablecopy会生成新的对象,copy则会只复制指针。

    相关文章

      网友评论

          本文标题:iOS NSMutableArray和NSArray实例执行mu

          本文链接:https://www.haomeiwen.com/subject/tozvxktx.html