[Objective-C]strong、copy在NSArray和NSMutableArray的应用区别

两个问题

1. 使用strong修饰NSArray会有什么问题?

2. 使用copy修饰NSMutableArray会有什么问题?


1. 使用strong修饰NSArray的问题

 //
//  StrongCopyTest.h
//  StudyRuntime
//
//  Created by Andy Liu on 2018/5/14.
//  Copyright © 2018年 Andy Liu. All rights reserved.
//

#import 

@interface StrongCopyTest : NSObject

@property (nonatomic,strong) NSArray *immutableArray;

@property (nonatomic,strong) NSMutableArray *mutableArray;

- (void)doTest;

@end
//
//  StrongCopyTest.m
//  StudyRuntime
//
//  Created by Andy Liu on 2018/5/14.
//  Copyright © 2018年 Andy Liu. All rights reserved.
//

#import "StrongCopyTest.h"

@implementation StrongCopyTest

- (void)doTest
{
    self.mutableArray = [NSMutableArray arrayWithObject:@"111"];
    self.immutableArray = [NSArray array];
    
    NSLog(@"before mutableArray=%@,immutableArray=%@",self.mutableArray,self.immutableArray);
    
    self.immutableArray = self.mutableArray;
    [self.mutableArray addObject:@"222"];
    
    NSLog(@"after mutableArray=%@,immutableArray=%@",self.mutableArray,self.immutableArray);
    
}
@end

输出结果如下

2018-05-14 17:28:15.872188+0800 StudyRuntime[861:40479] before mutableArray=(
    111
),immutableArray=(
)
2018-05-14 17:28:15.872308+0800 StudyRuntime[861:40479] after mutableArray=(
    111,
    222
),immutableArray=(
    111,
    222
)
Program ended with exit code: 0

可以看出,immutabeArray和mutableArray都被修改了,immutableArray不是预料的结果。

原因分析:

self.immutableArray = self.mutableArray;这行代码将可变数组的指针赋值给了不可变数组指针,由于OC中都是C的指针操作,并且是弱类型,因此这是合法的,编译器不会报错。甚至一个 id x = self.mutableArray也是可以的,因此弱类型是有潜在风险的。此时immutableArray和mutableArray都是指向了同一个内存地址,即mutableArray的内存地址,因此后面修改mutableArray的数据也会导致immutableArray的数据修改,因为是同一块内存地址。
这是一个陷阱。

2. 使用copy修饰NSMutableArray会有什么问题?

//
//  StrongCopyTest.h
//  StudyRuntime
//
//  Created by Andy Liu on 2018/5/14.
//  Copyright © 2018年 Andy Liu. All rights reserved.
//

#import 

@interface StrongCopyTest : NSObject

//@property (nonatomic,copy) NSArray *immutableArray;

@property (nonatomic,copy) NSMutableArray *mutableArray;

- (void)doTest;

@end
//
//  StrongCopyTest.m
//  StudyRuntime
//
//  Created by Andy Liu on 2018/5/14.
//  Copyright © 2018年 Andy Liu. All rights reserved.
//

#import "StrongCopyTest.h"

@implementation StrongCopyTest

- (void)doTest
{
    self.mutableArray = [NSMutableArray arrayWithObject:@"111"];
//    self.immutableArray = [NSArray array];
//    
//    NSLog(@"before mutableArray=%@,immutabxrleArray=%@",self.mutableArray,self.immutableArray);
//    
//    self.immutableArray = self.mutableArray;
    [self.mutableArray addObject:@"222"];
    
    //NSLog(@"after mutableArray=%@,immutableArray=%@",self.mutableArray,self.immutableArray);
    
}
@end

输出结果如下

2018-05-14 17:59:37.794905+0800 StudyRuntime[944:61946] -[__NSSingleObjectArrayI addObject:]: unrecognized selector sent to instance 0x10061c540
2018-05-14 17:59:37.796059+0800 StudyRuntime[944:61946] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSSingleObjectArrayI addObject:]: unrecognized selector sent to instance 0x10061c540'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff2a17032b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fff517eac76 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff2a208e04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x00007fff2a0e6870 ___forwarding___ + 1456
    4   CoreFoundation                      0x00007fff2a0e6238 _CF_forwarding_prep_0 + 120
    5   StudyRuntime                        0x0000000100001b91 -[StrongCopyTest doTest] + 161
    6   StudyRuntime                        0x0000000100001a85 testStrongCopy + 53
    7   StudyRuntime                        0x0000000100001ad5 main + 53
    8   libdyld.dylib                       0x00007fff52404015 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

原因分析:

self.mutableArray = [NSMutableArray arrayWithObject:@"111"];这句代码将得到一个不可变的数组(copy属性导致的).在此处打一个断点,然后输出其数据类型:

(lldb) po self.mutableArray
<__NSSingleObjectArrayI 0x102a01ed0>(
111
)

后面的异常就显而易见了。

self.mutableArray = [NSMutableArray arrayWithObject:@"111"];
这句话相当于

NSMutableArray *arr =  [NSMutableArray arrayWithObject:@"111"];
self.mutableArray = [arr copy];

正确的代码

@property (nonatomic,copy) NSArray *immutableArray;

@property (nonatomic,strong) NSMutableArray *mutableArray;

其他集合

NSArray,NSDictionary,NSSet -> copy
NSMutableArray,NSMutableDictionary,NSMutableSet->strong

你可能感兴趣的:([Objective-C]strong、copy在NSArray和NSMutableArray的应用区别)