简体   繁体   中英

How to set NSMutableString to null?

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?

How about str = nil ?

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

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.

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] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM