I've read that stringWithFormat and (alloc) initWithString are basically the same except obviously you have to [release] the alloc init yourself and stringWithFormat is retained.
My problem however is how to use these in a for loop effectively. I could do it this way...
However from what I understand it's using a a lot of memory because esentially each time through the loop its increasing the retain count.
If I did it this way it seems like it is equally if not more inefficient:
so i'm alloc init everytime through the for loop and releasing at the end and repeating... this doesn't seem like good coding practice to me. maybe there would potential for memory leaks, although it seems like there is more potential for memory leaks with the 1st code example than the 2nd?
any thoughts on this would be helpful. I know people would suggest alloc/init before the for loop but this wouldn't be helpful because the for loop is what will populate the string with it's data.
My problem however is how to use these in a for loop effectively. I could do it this way...
Code:
for (int x = 1, blah, blah) {
do something
blah blah stringWithFormat
do something
}
other code
However from what I understand it's using a a lot of memory because esentially each time through the loop its increasing the retain count.
If I did it this way it seems like it is equally if not more inefficient:
Code:
for (int x = 1, blah, blah) {
do something
blah blah alloc initwithformat blah blah
do something
[release mystring];
}
other code
so i'm alloc init everytime through the for loop and releasing at the end and repeating... this doesn't seem like good coding practice to me. maybe there would potential for memory leaks, although it seems like there is more potential for memory leaks with the 1st code example than the 2nd?
any thoughts on this would be helpful. I know people would suggest alloc/init before the for loop but this wouldn't be helpful because the for loop is what will populate the string with it's data.
Last edited: