Technology Sharing

copy and mutableCopy are a bit confusing

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Perform a copy on a string literal

To print the address of the object pointed to by a pointer and the address of the pointer itself, you can use %p The following code showsoriginalString andcopiedString The pointer address and the address of the pointed object:

NSString *originalString = @"Hello, World!";
NSString *copiedString = [originalString copy];

// 打印字符串内容
NSLog(@"Original: %@", originalString); // 输出: Hello, World!
NSLog(@"Copied: %@", copiedString);     // 输出: Hello, World!

// 打印指针本身的地址
NSLog(@"Original pointer address: %p", &originalString);
NSLog(@"Copied pointer address: %p", &copiedString);

// 打印指针指向对象的地址
NSLog(@"Original object address: %p", originalString);
NSLog(@"Copied object address: %p", copiedString);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Xcode Version 15.1 (15C65) & simulator (iPhone 15) running results are as follows:

// 打印的内容一样
Original: Hello, World!
Copied: Hello, World!
// 指针本地的地址不一样,说明是不同的指针
Original pointer address: 0x7ff7b1a5fa98
Copied pointer address: 0x7ff7b1a5fa90
// 不同的指针指向同一个对象
Original object address: 0x10e4a0070
Copied object address: 0x10e4a0070
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

explain

  • Original: Hello, World! andCopied: Hello, World! Output string content.
  • Original pointer address: %p Output pointeroriginalString Its own address on the stack.
  • Copied pointer address: %p Output pointercopiedString Its own address on the stack.
  • Original object address: %p OutputoriginalString The address of the string object pointed to in the heap.
  • Copied object address: %p OutputcopiedString The address of the string object pointed to in the heap.

It can be seen that due to NSString is an immutable object, executingcopy method does not create a new object, but returns the same object.originalString andcopiedString They point to the same memory address (the same object address), but their pointers themselves have different addresses in the stack.


Perform mutableCopy on string literal

    NSString *originalString = @"Hello, World!";
    NSString *copiedString = [originalString mutableCopy];

    // 打印字符串内容
    
    NSLog(@"Original: %@", originalString); // 输出: Hello, World!
    NSLog(@"Copied: %@", copiedString);     // 输出: Hello, World!

    // 打印指针本身的地址
    NSLog(@"Original pointer address: %p", &originalString);
    NSLog(@"Copied pointer address: %p", &copiedString);

    // 打印指针指向对象的地址
    NSLog(@"Original object address: %p", originalString);
    NSLog(@"Copied object address: %p", copiedString);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

Xcode Version 15.1 (15C65) & simulator (iPhone 15) running results are as follows:

Original: Hello, World!
Copied: Hello, World!
// 不同的指针
Original pointer address: 0x7ff7be102a98
Copied pointer address: 0x7ff7be102a90
// 指向不同的对象
Original object address: 0x101dfd070
Copied object address: 0x600000c80090
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8