xcode版本:8.3.3
//
// ViewController.m
// 内存管理
//
// Created by walg on 2017/8/9.
// Copyright © 2017年 walg. All rights reserved.
//
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
@property (nonatomic, strong) NSMutableString *strM;
@property (nonatomic, strong) NSString *str;
@property (nonatomic, copy) NSString *strC1;
@property (nonatomic, copy) NSString *strC2;
@property (nonatomic, copy) NSMutableString *strC3;
@property (nonatomic, copy) NSMutableString *strC4;
@property (nonatomic, strong) NSString *strS1;
@property (nonatomic, strong) NSString *strS2;
@property (nonatomic, strong) NSMutableString *strS3;
@property (nonatomic, strong) NSMutableString *strS4;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// self.strM = [NSString stringWithFormat:@"abc"];
self.strM = [NSMutableString stringWithFormat:@"abc"];
self.strC1 = self.strM;
self.strS1 = self.strM;
self.strC3 = self.strM;
self.strS3 = self.strM;
self.strC2 = [NSString stringWithFormat:@"%@",self.strM];
self.strS2 = [NSString stringWithFormat:@"%@",self.strM];
self.strC4 = (NSMutableString*)[NSString stringWithFormat:@"%@",self.strM];
self.strS4 = (NSMutableString*)[NSString stringWithFormat:@"%@",self.strM];
NSLog(@"---------->>>开始<<<----------");
NSLog(@"Ori_address----->>>>>>:value:%@---%p==%p",_strM,_strM,&_strM);
NSLog(@"address----->>>>>>C1:value:%@---%p==%p",_strC1,_strC1,&_strC1);
NSLog(@"address----->>>>>>S1:value:%@---%p==%p",_strS1,_strS1,&_strS1);
NSLog(@"address----->>>>>>C2:value:%@---%p==%p",_strC2,_strC2,&_strC2);
NSLog(@"address----->>>>>>S2:value:%@---%p==%p",_strS2,_strS2,&_strS2);
NSLog(@"address----->>>>>>C3:value:%@---%p==%p",_strC3,_strC3,&_strC3);
NSLog(@"address----->>>>>>S3:value:%@---%p==%p",_strS3,_strS3,&_strS3);
NSLog(@"address----->>>>>>C4:value:%@---%p==%p",_strC4,_strC4,&_strC4);
NSLog(@"address----->>>>>>S4:value:%@---%p==%p",_strS4,_strS4,&_strS4);
NSLog(@"---------->>>结束<<<----------\n");
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// self.strM = [_strM stringByAppendingString:@"de"];
[_strM appendString:@"de"];
NSLog(@"---------->>>点击开始输出<<<----------");
NSLog(@"Ori_address<<<<<<----->>>>>>:value:%@---%p==%p",_strM,_strM,&_strM);
NSLog(@"address<<<<<<----->>>>>>C1:value:%@---%p==%p",_strC1,_strC1,&_strC1);
NSLog(@"address<<<<<<----->>>>>>S1:value:%@---%p==%p",_strS1,_strS1,&_strS1);
NSLog(@"address<<<<<<----->>>>>>C2:value:%@---%p==%p",_strC2,_strC2,&_strC2);
NSLog(@"address<<<<<<----->>>>>>S2:value:%@---%p==%p",_strS2,_strS2,&_strS2);
NSLog(@"address<<<<<<----->>>>>>C3:value:%@---%p==%p",_strC3,_strC3,&_strC3);
NSLog(@"address<<<<<<----->>>>>>S3:value:%@---%p==%p",_strS3,_strS3,&_strS3);
NSLog(@"address<<<<<<----->>>>>>C4:value:%@---%p==%p",_strC4,_strC4,&_strC4);
NSLog(@"address<<<<<<----->>>>>>S4:value:%@---%p==%p",_strS4,_strS4,&_strS4);
NSLog(@"---------->>>点击结束输出<<<----------\n");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
《点了两次》
2017-08-09 18:17:29.112 内存管理[31855:14351521] ---------->>>开始<<<----------
2017-08-09 18:17:29.112 内存管理[31855:14351521] Ori_address----->>>>>>:value:abc---0x60800026ba40==0x7f86d0421748
2017-08-09 18:17:29.112 内存管理[31855:14351521] address----->>>>>>C1:value:abc---0xa000000006362613==0x7f86d0421758
2017-08-09 18:17:29.113 内存管理[31855:14351521] address----->>>>>>S1:value:abc---0x60800026ba40==0x7f86d0421778
2017-08-09 18:17:29.113 内存管理[31855:14351521] address----->>>>>>C2:value:abc---0xa000000006362613==0x7f86d0421760
2017-08-09 18:17:29.113 内存管理[31855:14351521] address----->>>>>>S2:value:abc---0xa000000006362613==0x7f86d0421780
2017-08-09 18:17:29.113 内存管理[31855:14351521] address----->>>>>>C3:value:abc---0xa000000006362613==0x7f86d0421768
2017-08-09 18:17:29.113 内存管理[31855:14351521] address----->>>>>>S3:value:abc---0x60800026ba40==0x7f86d0421788
2017-08-09 18:17:29.113 内存管理[31855:14351521] address----->>>>>>C4:value:abc---0xa000000006362613==0x7f86d0421770
2017-08-09 18:17:29.114 内存管理[31855:14351521] address----->>>>>>S4:value:abc---0xa000000006362613==0x7f86d0421790
2017-08-09 18:17:29.114 内存管理[31855:14351521] ---------->>>结束<<<----------
2017-08-09 18:19:13.818 内存管理[31855:14351521] ---------->>>点击开始输出<<<----------
2017-08-09 18:19:13.819 内存管理[31855:14351521] Ori_address<<<<<<----->>>>>>:value:abcde---0x60800026ba40==0x7f86d0421748
2017-08-09 18:19:13.820 内存管理[31855:14351521] address<<<<<<----->>>>>>C1:value:abc---0xa000000006362613==0x7f86d0421758
2017-08-09 18:19:13.820 内存管理[31855:14351521] address<<<<<<----->>>>>>S1:value:abcde---0x60800026ba40==0x7f86d0421778
2017-08-09 18:19:13.820 内存管理[31855:14351521] address<<<<<<----->>>>>>C2:value:abc---0xa000000006362613==0x7f86d0421760
2017-08-09 18:19:13.820 内存管理[31855:14351521] address<<<<<<----->>>>>>S2:value:abc---0xa000000006362613==0x7f86d0421780
2017-08-09 18:19:13.820 内存管理[31855:14351521] address<<<<<<----->>>>>>C3:value:abc---0xa000000006362613==0x7f86d0421768
2017-08-09 18:19:13.820 内存管理[31855:14351521] address<<<<<<----->>>>>>S3:value:abcde---0x60800026ba40==0x7f86d0421788
2017-08-09 18:19:13.821 内存管理[31855:14351521] address<<<<<<----->>>>>>C4:value:abc---0xa000000006362613==0x7f86d0421770
2017-08-09 18:19:13.845 内存管理[31855:14351521] address<<<<<<----->>>>>>S4:value:abc---0xa000000006362613==0x7f86d0421790
2017-08-09 18:19:13.845 内存管理[31855:14351521] ---------->>>点击结束输出<<<----------
2017-08-09 18:19:19.625 内存管理[31855:14351521] ---------->>>点击开始输出<<<----------
2017-08-09 18:19:19.626 内存管理[31855:14351521] Ori_address<<<<<<----->>>>>>:value:abcdede---0x60800026ba40==0x7f86d0421748
2017-08-09 18:19:19.626 内存管理[31855:14351521] address<<<<<<----->>>>>>C1:value:abc---0xa000000006362613==0x7f86d0421758
2017-08-09 18:19:19.626 内存管理[31855:14351521] address<<<<<<----->>>>>>S1:value:abcdede---0x60800026ba40==0x7f86d0421778
2017-08-09 18:19:19.626 内存管理[31855:14351521] address<<<<<<----->>>>>>C2:value:abc---0xa000000006362613==0x7f86d0421760
2017-08-09 18:19:19.626 内存管理[31855:14351521] address<<<<<<----->>>>>>S2:value:abc---0xa000000006362613==0x7f86d0421780
2017-08-09 18:19:19.627 内存管理[31855:14351521] address<<<<<<----->>>>>>C3:value:abc---0xa000000006362613==0x7f86d0421768
2017-08-09 18:19:19.627 内存管理[31855:14351521] address<<<<<<----->>>>>>S3:value:abcdede---0x60800026ba40==0x7f86d0421788
2017-08-09 18:19:19.627 内存管理[31855:14351521] address<<<<<<----->>>>>>C4:value:abc---0xa000000006362613==0x7f86d0421770
2017-08-09 18:19:19.628 内存管理[31855:14351521] address<<<<<<----->>>>>>S4:value:abc---0xa000000006362613==0x7f86d0421790
2017-08-09 18:19:19.628 内存管理[31855:14351521] ---------->>>点击结束输出<<<----------
可以看到:
一、在接收NSMutableString的值时:
(1)@property (nonatomic, strong) NSString *strS2; 用“=”赋值时,_strS2 与_strC3的值指向同一内存地址。当_strC3改变时,_strS2也跟着改变。
(2)@property (nonatomic, copy) NSString *strC2; 用“=”赋值时,_strC2 与_strC3的值指向新的内存地址。当_strC3改变时,_strC2不跟着改变。
(3)[NSString stringWithFormat:@"%@",self.strC3]当用这种方式赋值时,无论是copy还是strong都会开辟新的地址,与原来的都没有关联。
(4)以上跟NSMutableString还是NSString无关。
二、在接收NSString的值时,都是开辟新的地址。互不影响,与copy和strong无关!
网友评论