ios 变量 copy 与 mutableCopy

#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个变量的地址也都是不一样的。
相关文章
相关标签/搜索