Tuesday, September 24, 2013

NSString with questions

NSString * str = [[NSString alloc] initWithFormat: @ "aaaaa"];

NSString * temp = [str subStringFromIndex: 3];
[Temp uppercaseString];

/ * Here right temp operate on it?

If the above two code into the following, there is no need? Where is the difference?

NSString * temp = [str subStringFromIndex: 3];
[Temp retain];
[Temp uppercaseString];
[Temp release];
* /

[Str release];

Reply:
1. UppercaseString does not modify the original string, it will generate a new string. Should therefore be so used:
NSString * str2 = [temp substringFromIndex];

2. SubstringFromIndex and substringFromIndex string generated without their release, it is automatically released.

Suggested wording:
 
{
NSString * str = [NSString stringWithFormat: @ "aaaaa"];
NSString * temp = [str substringFromIndex: 3];
NSString * str2 = [temp uppercaseString];
}

No comments:

Post a Comment