IOS easy to forget face test collection (preparation)

Source: Internet
Author: User
Tags shallow copy

1, why NSString to use copy. Rather than strong/assign.

Case 1:

@interface Test ()
  @property (nonatomic, strong) NSString *strongstring;
  @property (nonatomic, copy) NSString *copyedstring;
@end
@implementation

-(void) test{
    nsstring *string = [NSString stringwithformat:@ "abc"];
    self.strongstring = string;
    self.copyedstring = string;
    NSLog (@ "Origin string:%p,%p", String, &string);
    NSLog (@ "Strong string:%p,%p", _strongstring, &_strongstring);
    NSLog (@ "copy string:%p,%p", _copyedstring, &_copyedstring);

@end

Results:

Origin String:0x7fe441592e20, 0x7fff57519a48
strong string:0x7fe441592e20, 0x7fe44159e1f8
copy string: 0X7FE441592E20, 0x7fe44159e200

Case 2:

@interface Test ()
  @property (nonatomic, strong) NSString *strongstring;
  @property (nonatomic, copy) NSString *copyedstring;
@end
@implementation

-(void) test{
    nsmutablestring *string = [nsmutablestring stringwithformat:@ "abc"];
    self.strongstring = string;
    self.copyedstring = string;
    NSLog (@ "Origin string:%p,%p", String, &string);
    NSLog (@ "Strong string:%p,%p", _strongstring, &_strongstring);
    NSLog (@ "copy string:%p,%p", _copyedstring, &_copyedstring);
    Will something unexpected happen?
    [string appendstring:@ "333"];
    NSLog (@ "Origin string:%@",  mstr);
    NSLog (@ "Strong string:%@", _rstr);
    NSLog (@ "Copy string:%@",   _cstr);
}

@end

Results:

Origin String:0x7fe441592e20, 0x7fff57519a48
strong string:0x7fe441592e20, 0x7fe44159e1f8
copy string: 0X7FE441592E20, 0x7fe44159e200
origin string:abc333
strong string:abc333
copy STRING:ABC

Conclusion
1. When the source data is the NSString type, the copy/strong/assign result is the same, both copies of the object address are shallow copies
2. When the source data is a nsmutablestring type, using the Copy object address change is a new object, belonging to a deep copy. Strong/assign address Same, shallow copy
3. So when modifying a string, use copy as much as possible to avoid unexpected problems.

Finally, I recommend a simple book that I feel summed up very well: https://www.jianshu.com/p/2e1b3f54b4f3

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.