#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) NSMutableString *t_ori;
@property (nonatomic, strong) NSMutableString *t_copy;
@property (nonatomic, strong) NSMutableString *t_mCopy;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.t_ori = [NSMutableString stringWithString:@"test"];
self.t_copy = [self.t_ori copy];
self.t_mCopy = [self.t_ori mutableCopy];
}
@end
中如果类型是mutable的,copy, mutableCopy 后变量所只想的地址都是不同的。
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) NSString *t_ori;
@property (nonatomic, strong) NSString *t_copy;
@property (nonatomic, strong) NSString *t_mCopy;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.t_ori = @"test";
self.t_copy = [self.t_ori copy];
self.t_mCopy = [self.t_ori mutableCopy];
}
@end
如果是不可变类型的,则copy 后 self.t_copy 与 self.t_ori 指向的是同一个地址, self.t_mCopy则是不同的地址。
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) NSString *t_ori;
@property (nonatomic, strong) NSString *t_copy;
@property (nonatomic, strong) NSString *t_mCopy;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.t_ori = [NSMutableString stringWithString:@"teest"];
self.t_copy = [self.t_ori copy];
self.t_mCopy = [self.t_ori mutableCopy];
}
@end
这样写的话,3个变量的地址也都是不同的。