0

Is it necessary to do instantiation for a new string object as the following code shows:

NSString *newText = [[NSString alloc] initWithFormat:@"%@",sender.titleLabel.text];

Or can we simply run the following code:

NSString *newText = sender.titleLabel.text;

which I believe will return the same result. So when do we know if "alloc" and "init" is required and when they are not?

Thanks

Zhen

Zhen
  • 12,361
  • 38
  • 122
  • 199

2 Answers2

4

You can simply use the assignment (newText = sender.titleLabel.text;).

The results of your two examples are not the same, BTW: in your first example you create a new object, in the second one you reuse an existing one. In the first example, you need to later on call [newText release]; (or autorelease), in your second example you may not.

If you intend to store the string in an instance variable you should copy it (myInstanceVariable = [sender.titleLabel.text copy];). The reason is because it might be an instance of NSMutableString which can change and thus yield unexpected behavior.

DarkDust
  • 90,870
  • 19
  • 190
  • 224
  • Hi, thanks for your response. Can I just clarify on the point whereby you mentioned that I am reusing the existing object. When I write NSString *newText = sender.titleLabel.text, am I not creating a new instance of NSString? I do not have the variable newText available in my code before this point of time. Thanks! – Zhen Apr 04 '11 at 12:32
  • @Zhen: Your variable `newText` is of type `pointer to NSString`. This means: there is a memory area that represents a NSString, and a pointer "points" to that area. Now, when you do `newText = sender.titleLabel.text` then your variable points to the same memory area/object as `newText = sender.titleLabel.text`. This is totally basic stuff, you really really need to understand it. Read up on pointers (C or Objective-C) and memory management in Objective-C, e.g. [this guide](http://www.raywenderlich.com/2657/memory-management-in-objective-c-tutorial). – DarkDust Apr 04 '11 at 12:41
  • thanks alot for your advise and help on this. I will take a deeper dive on this topic to have a better understanding. Thanks for the guide! – Zhen Apr 04 '11 at 13:13
2

When you use:

NSString *newText = sender.titleLabel.text;

you are just setting a pointer to the existing object at sender.titleLabel.text. You are telling the compiler that this new pointer points to an object of type NSString.

Note: the pointers newText and sender.titleLabel.txt now both point to the same object so changes made to the underlying object (such as changing the text) will be reflect when you access the object using either pointers.

Note 2: when you used:

NSString *newText = [[NSString alloc] initWithFormat:@"%@",sender.titleLabel.text];

You created an entirely new object (by using alloc) and then you init'd this new object with the string value of sender.titleLabel.text at the time the alloc operation was executed. Now newText and sender.titleLabel.text are two totally different NSString objects that are not related in anyway to each other and can be changed/managed/used/dealloc'd completely independently of each other.

Damien
  • 2,421
  • 22
  • 38
  • thanks. Are there any downsides to using this approach as opposed to creating a new object? – Zhen Apr 04 '11 at 12:57
  • @Zhen. To be honest the answer is: it depends! I know, not what you wanted to hear :-) It depends on if you want newText to be completely independent of sender.titleLabel.text. What are you trying to do and I'll be able to help more? – Damien Apr 04 '11 at 13:02
  • I am actually in the process of learning the language and not really creating something. Thanks for your help :) I will let you know when I know what I am creating! – Zhen Apr 04 '11 at 13:15
  • The retain count of `sender.titleLabel.text` is only changed when setting, not when getting. – DarkDust Apr 04 '11 at 13:25