#import "ViewController.h"
@interface Viewcontroller ()
@property (retain,nonatomic) NSString *myretainstr;
@property (copy, nonatomic) NSString *mycopystr;
@property (Strong, nonatomic) NSString *mystrongstr;
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
[Self testnsmutablestringcopyretain];
[Self testnsstringcopyretain];
[Self testnsstringstrongretain];
[Self testnsmutablestringstrongretain];
Conclusion
Retain and strong are the same for nsstrong and nsmutablestring effects. is pointing to an address.
The same is true for Nstring,retain and copy effects. All point to the same address.
The Nsmutablestring,retain and copy effects are not the same. Retain adds a reference count. Copy implements a deep copy.
Therefore, nstring and nsmutablestring are perfectly correct under normal conditions with the copy modifier.
}
Nsmutablestring's retain and copy differences
-(void) testnsmutablestringcopyretain{
nsmutablestring *mstr = [nsmutablestring stringwithformat:@ "abc"];
Self.myretainstr = MSTR;
Self.mycopystr = MSTR;
NSLog (@ "mstr:%p,%p", mstr,&mstr);
NSLog (@ "retainstr:%p,%p", _myretainstr, &_MYRETAINSTR);
NSLog (@ "copystr:%p,%p", _mycopystr, &_MYCOPYSTR);
2015-06-10 14:49:38.757 Strong retain copy nsstring difference [6812:359227] Mstr:0x7fed98536cd0,0x7fff56a199b8
2015-06-10 14:49:38.758 Strong retain copy nsstring difference [6812:359227] Retainstr:0x7fed98536cd0,0x7fed98541fd8
2015-06-10 14:49:38.758 Strong retain copy nsstring difference [6812:359227] Copystr:0x7fed9853db50,0x7fed98541fe0
As seen from the running results, the reference count is added for nsmutablestring retain. Copy is a deep copy
}
NSString's retain and copy differences
-(void) testnsstringcopyretain{
NSString *mstr = [NSString stringwithformat:@ "abc"];
Self.myretainstr = MSTR;
Self.mycopystr = MSTR;
NSLog (@ "mstr:%p,%p", mstr,&mstr);
NSLog (@ "retainstr:%p,%p", _myretainstr, &_MYRETAINSTR);
NSLog (@ "copystr:%p,%p", _mycopystr, &_MYCOPYSTR);
2015-06-10 14:53:19.822 Strong retain copy nsstring difference [6847:361075] Mstr:0x7fbc00c44c30,0x7fff547b69b8
2015-06-10 14:53:19.822 Strong retain copy nsstring difference [6847:361075] retainstr:0x7fbc00c44c30,0x7fbc00d12d58
2015-06-10 14:53:19.822 Strong retain copy nsstring difference [6847:361075] Copystr:0x7fbc00c44c30,0x7fbc00d12d60
Judging from the running results, for NSString, retain is adding a reference count. Copy is a shallow copy. There's no difference between them.
}
The difference between retain and strong of nsstring
-(void) testnsstringstrongretain{
NSString *mstr = [NSString stringwithformat:@ "abc"];
Self.myretainstr = MSTR;
Self.mystrongstr = MSTR;
NSLog (@ "mstr:%p,%p", mstr,&mstr);
NSLog (@ "retainstr:%p,%p", _myretainstr, &_MYRETAINSTR);
NSLog (@ "strongstr:%p,%p", _mystrongstr, &_MYSTRONGSTR);
2015-06-10 14:57:41.983 Strong retain copy nsstring difference [6919:363984] Mstr:0x7f894a49db90,0x7fff5aaf69b8
2015-06-10 14:57:41.983 Strong retain copy nsstring difference [6919:363984] Retainstr:0x7f894a49db90,0x7f894a543db8
2015-06-10 14:57:41.983 Strong retain copy nsstring difference [6919:363984] Strongstr:0x7f894a49db90,0x7f894a543dc8
Judging from the running results, for NSString, retain is adding a reference count. Strong is a shallow copy. The effect is the same. Point to the same address
}
The difference between retain and strong of nsmutablestring
-(void) testnsmutablestringstrongretain{
nsmutablestring *mstr = [nsmutablestring stringwithformat:@ "abc"];
Self.myretainstr = MSTR;
Self.mystrongstr = MSTR;
NSLog (@ "mstr:%p,%p", mstr,&mstr);
NSLog (@ "retainstr:%p,%p", _myretainstr, &_MYRETAINSTR);
NSLog (@ "strongstr:%p,%p", _mystrongstr, &_MYSTRONGSTR);
2015-06-10 15:01:54.719 Strong retain copy nsstring difference [6963:366057] Mstr:0x7f9033715590,0x7fff58f749b8
2015-06-10 15:01:54.720 Strong retain copy nsstring difference [6963:366057] Retainstr:0x7f9033715590,0x7f9033617e28
2015-06-10 15:01:54.720 Strong retain copy nsstring difference [6963:366057] Strongstr:0x7f9033715590,0x7f9033617e38
Judging from the running results, for NSString, retain is adding a reference count. Strong is a shallow copy. The effect is the same. Point to the same address
}
Strong retain copy for nsstring,nsmutablestring difference