NSString *string=[temp objectAtIndex:row];
[temp removeObjectAtIndex:row];
The object from which string is created no longer exists; I assume this syntax is straight assignment, thus string should no longer exist, right?
So either I’d use stringWithFormat to create a new string from the object, or send retain to string to be safe, is this correct reasoning?
Assuming you aren’t using ARC or garbage collection, yes, your reasoning about the object’s memory management is correct†. However, you wouldn’t want to use
stringWithFormat:. That expects a format string, and it can fail horribly if you pass something that isn’t specifically meant to be one. You could use one of:stringWithString:if you don’t want ownershipcopyif you just want to make sure you have a safe reference to the string (optionally withautoreleaseif you don’t plan on keeping the reference around long-term)retainif you definitely want to keep the same object — even if it’s a mutable string — and do want ownership (You probably don’t want this option, as the main difference fromcopyis that the string could potentially mutate underneath you)† By “correct”, I mean you’re right that you shouldn’t assume anything about the object’s lifecycle beyond that point. It might still be alive, but it’s technically not a safe assumption. Thought it worth mentioning because some people aren’t 100% clear on this point.