Home  >  Q&A  >  body text

objective-c - iOS 深浅拷贝?

NSString *i = @"1";
NSString *j = [i mutableCopy];
NSLog(@"i=%p,j=%p",i,j);

打印地址不一样,由此可见是内容拷贝
继续
i = @"2";
NSLog(@"i=%@,j=%@",i,j);
那么问题来了,为什么j的值还是1?
PHP中文网PHP中文网2727 days ago852

reply all(3)I'll reply

  • 天蓬老师

    天蓬老师2017-05-02 09:38:07

    Content copy is a deep copy. The new j generated is a new object and has nothing to do with i. So changing i will not affect j.

    reply
    0
  • 为情所困

    为情所困2017-05-02 09:38:07

    The addresses are different, which means they are two objects. So the question is, why are the values ​​​​between two independent objects still related?

    reply
    0
  • PHP中文网

    PHP中文网2017-05-02 09:38:07

    ok, let me check the information first

    reply
    0
  • Cancelreply