views:

53

answers:

3
NSMutableString *str;

//I have already done the allocation etc

Suppose this str has some value. I set str=@"" it shows a warning. How to set the NSMutableString as Null?

+1  A: 

How about str = nil?

You should also [str release] first in order to free the memory used by the current string.

Brian
A: 

You can use str = nil;

But in almost all cases, I would probably recommend you use NSString instead of NSMutableString. When you assign your str variable @"", that is instantiating a new empty string (NSString), and assigning it to str, hence the type error.

It is almost always easier to just create a new string from your old string using something like [NSString stringByAppendingString:@" new string content"] or [NSString stringByAppendingFormat:@"%@ stuff %@", otherString1, otherString2].

livingtech
A: 

If you are wanting to just set it to an empty string, do the following:

NSMutableString *str;

// append some strings // ...

[str setString:@""];

Otherwise, if you want to release it and set it to null:

[str release], str = nil;

Note: Constant strings like @"" uses special strings refs. You can look into this further, but its not actually allocating a new string every time.

bstahlhood