Object ownership in stringWithString and initWithString in NSString

后端 未结 5 469
小蘑菇
小蘑菇 2020-12-13 16:41

I understand that any init... method initializes a new object and that NSString stringWithString makes a copy of the parameter string as a new object. I also understand that

5条回答
  •  猫巷女王i
    2020-12-13 17:39

    In the Incorrect code above, the next time name is referenced after setName is called, you'll get an exception error, since the object will have been released. You can use either the "Correct" code, or wrap your stringWithString call in an explicit retain call:

    name = [[NSString stringWithString: theName] retain];
    

提交回复
热议问题