The difference between strong and copy in Objective-C

The difference between strong and copy in Objective-C

In Objective-C development, we often use strong and copy attribute modifiers. For NSString, the two have the same effect, but for NSMutableString, the two have different meanings. We can verify it through the code.

NSString

@property (nonatomic, strong) NSString *strongString;
@property (nonatomic, copy) NSString *copiedString;


@property (nonatomic, strong) NSMutableString *strongMutableString;
@property (nonatomic, copy) NSMutableString *copiedMutableString;

In the properties of the class, we add the above properties, NSString modified by strong and copy, and NSMutableString modified by strong and copy.

 NSString *string = [NSString stringWithFormat:@"hello string, hello world!"];
 self.strongString = string;
 self.copiedString = string;
    
 NSLog(@"%p", string);
 NSLog(@"%p", self.strongString);
 NSLog(@"%p", self.copiedString);

Generate an NSString instance and assign it, and assign string to copiedString and strongString respectively. After running the program, it can be found that the memory addresses of the three are the same. Here %p is to print the address of the instance.

iOSProject[71752:7608123] 0x600000164f00
iOSProject[71752:7608123] 0x600000164f00
iOSProject[71752:7608123] 0x600000164f00

The memory addresses of the three are the same, which means that strong and copy have the same effect on NSString.

NSMutableString

NSMutableString *mutableString = [[NSMutableString alloc] initWithFormat:@"hello mutableString, hello world!"];
self.strongMutableString = mutableString;
self.copiedMutableString = mutableString;
   
NSLog(@"%p", mutableString);
NSLog(@"%p", self.strongMutableString);
NSLog(@"%p", self.copiedMutableString);

Write the NSMutableString code, and assign mutableString to strongMutableString and copiedMutableString respectively. After running the program, the memory addresses of the three are not exactly the same. mutableString and strongMutableString are the same memory address, but copiedMutableString is a different memory address, and copiedMutableString is copied during the assignment process.

iOSProject[71842:7610995] 0x600002e0f330
iOSProject[71842:7610995] 0x600002e0f330
iOSProject[71842:7610995] 0x60000350d580
  [mutableString replaceOccurrencesOfString:@", hello world!" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, mutableString.length)];
  NSLog(@"%@", mutableString);
  NSLog(@"%@", self.strongMutableString);
  NSLog(@"%@", self.copiedMutableString);

Modify the code, replace the string content of mutableString, and then print the values ​​of the three instance objects, you can find

The value of mutableString has changed, and the value of strongMutableString has also changed. copiedMutableString has not changed, because it points to another memory address.

iOSProject[71842:7610995] hello mutableString
iOSProject[71842:7610995] hello mutableString
iOSProject[71842:7610995] hello mutableString, hello world!

Summarize

For NSString, strong and copy have the same function, and both make the properties point to the same memory.

For NSMutableString, strong points to the same memory, while copy points to the newly generated memory.

In actual development, NSString can also point to NSMutableString. If you don't want to modify the original memory, you can use copy to modify the attribute at this time.

Guess you like

Origin blog.csdn.net/u011608357/article/details/128525340