2

Please give me some suggestions about how to change a NSString variable.

At my class, I set a member var:

NSString *m_movieName;
...
@property(nonatomic, retain) NSString *m_movieName;

At viewDidLoad method, I assign a default name to this var:

-(void)viewDidLoad{
NSString *s1 = [[NSString alloc] initWithFormat:@"Forrest Gump"];
self.m_movieName = s1;
...
[s1 release];
[super viewDidLoad]
}

At some function, I want to give a new name to this var, so I did like:

-(void)SomeFunc{
NSString *s2 = [[NSString alloc] initWithFormat:@"Brave Heart"];
//[self.movieName release]; // ??????? Should perform here?
self.m_moiveName = s2;
[s2 release];
}

I know, NSString* var is just a pointer to an allocated memory block, and 'assign' operation will increment this memory block's using count. For my situation, should I release m_movieName before assigning a value to it? If I do not release it (via [self.movieName release]), when and where will the previous block be released? Thanks for your help very much!

Elliot Chen
  • 378
  • 2
  • 9
  • http://stackoverflow.com/questions/2922196/best-way-to-initialise-clear-a-string-variable-cocoa/2922232#2922232 This one can help you – vodkhang Jun 06 '10 at 12:42

2 Answers2

7

No, you have declared your property as retain, which means your object will retain the properties value. The synthesized (=compiler generated) setter will take care of releasing the old NSString before assigning a new value, so you don't have to manage this yourself.

Be careful when accessing the backing store for the property directly (m_movieName in your instance) as bypassing the setter won't release the current value automatically.

Johannes Rudolph
  • 35,298
  • 14
  • 114
  • 172
  • Wow, Johannes, Thanks for so quick an answer!! – Elliot Chen Jun 06 '10 at 12:42
  • I have to accept your answer 10 minutes later under system restrain. Thanks again. – Elliot Chen Jun 06 '10 at 12:44
  • @Elliot: Pablo's advice is valuable though, make sure to check it out too (even though it doesn't answer your question directly). – Johannes Rudolph Jun 06 '10 at 12:46
  • 1
    Elliott Chen: Pay careful attention to the latter paragraph of Johannes's answer. Assigning directly to the instance variable (`m_movieName = …`) is very different from assigning to the property (`self.m_movieName = …`). Specifically, assigning to the instance variable has *no* side effects: The previous value is not released, and the new value is not retained or copied. You are simply replacing one pointer with another, nothing more. Only assigning to the property triggers side effects such as memory management. – Peter Hosey Jun 06 '10 at 20:53
  • Hi, Peter, this point is new to me and it's so useful and important. Thanks for your help! And thanks all of you. – Elliot Chen Jun 07 '10 at 01:53
3

First of all, my advice would be, if you are using a NSString property, use copy instead of retain. Check this SO question out for details.

@property(nonatomic, copy) NSString *m_movieName;
Community
  • 1
  • 1
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292