0

sText is an instance variable of type NSString. No properties are involved.

The loop below is supposed to get the first character of sText and store that char in sCurrentCharacter. Then the rest of sText should be reassigned to sText. This goes on until the string has been eaten up. (I left out all the code which actually does something sensible to the variables).

while ([sText length] > 0)
{
// Get the current first ASCII character.
NSString *sCurrentCharacter = [sMorseText substringToIndex:1];
// Keep everything but the first character.
sText = [sText substringFromIndex:1];
}

Questions: do I have to release sText before reassigning the result of "substringFromIndex"? If yes: why? If not: why not? :-)

René

2
  • i hv 5 objects in array.. Now i want get back as in string like Object 1 and object 2 and object 3 and object 4 object 5.. Which's correct way..? Please help me.. Commented Mar 31, 2011 at 9:18
  • 1
    You should ask a proper question, but: NSArray *array1 = [NSArray arrayWithObjects:@"1", @"2", @"3", nil]; NSString *joinedString = [array1 componentsJoinedByString:@","]; Commented Mar 31, 2011 at 11:03

2 Answers 2

3

That depends on how you created the initial sText. If sText is an autoreleased object or you have not explicitly retained it, then you are fine. If you created sText using init, copy or retained it, then you should autorelease it before the while loop.

so this is fine:

NSString *sText = @"foo";
while ([sText length] > 0){
   // Get the current first ASCII character.
   NSString *sCurrentCharacter = [sMorseText substringToIndex:1];
  // Keep everything but the first character.
  sText = [sText substringFromIndex:1];
}

This will leak the original value of sText:

NSString *sText = [myString retain];
while ([sText length] > 0){
   // Get the current first ASCII character.
   NSString *sCurrentCharacter = [sMorseText substringToIndex:1];
  // Keep everything but the first character.
  sText = [sText substringFromIndex:1];
}
Sign up to request clarification or add additional context in comments.

4 Comments

As a corollary, all of the later values of sText—those created within the loop—do not need to be released, because you neither created them by alloc, retained them, nor created them by sending copy or mutableCopy to another object.
I know the rule about alloc, copy etc. but still it looks weird to me. I mean: I init sText using sText = @"SomeText"; Then I assign it a new value using sText = [sText substring:1]. substring will return a new instance of a string (I suppose) and will overwrite sText (which is just a pointer to the text). Now the old text should be somewhere in memory and never be deallocated, no?
The old value will be deallocated when the current autorelease pool is popped.
But I have the impression that I lose the contents of my NSString. If NSString *sText is a class member and in viewDidLoad I set it to 'sText = @"sometext";' and then whenever a timer fires I strip the first character and replace sText with the remaining characters using substring. On the second timer call I get a program abortion. If I retain sText, it won't abort. I think this is because the autorelease pool is released at the end of the timer method, correct?
0

Memory management issues aside, this is a rather inefficient approach in Objective-C (but great for LISP!). A more efficient approach would be:

NSUInteger iter, length = sText.length; // Only call -length once.
for (iter = 0; iter < length; iter++)
{
    unichar c = [sText characterAtIndex:iter];
    // Or, if you actually need it as an NSString:
    NSString *substr = [sText substringWithRange:(NSRange){ iter, 1 }];
}

2 Comments

Yeah, thanks but the sample was more to demonstrate the issue but no a real-life code fragment.
i hv 5 objects in array.. Now i want get back as in string like Object 1 and object 2 and object 3 and object 4 object 5.. Which's correct way..? Please help me..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.