i've read stringwithformat , (alloc) initwithstring same except have [release] alloc init , stringwithformat retained.
problem how use these in loop effectively. way...
code:
for (int x = 1, blah, blah) { blah blah stringwithformat } other code however understand it's using a lot of memory because esentially each time through loop increasing retain count.
if did way seems equally if not more inefficient:
code:
for (int x = 1, blah, blah) { blah blah alloc initwithformat blah blah [release mystring]; } other code so i'm alloc init everytime through loop , releasing @ end , repeating... doesn't seem coding practice me. maybe there potential memory leaks, although seems there more potential memory leaks 1st code example 2nd?
thoughts on helpful. know people suggest alloc/init before loop wouldn't helpful because loop populate string it's data.
look @ nsmutablestring. alloc , init 1 nsmutablestring before loop. append things directly in loop. when loop ends, have 1 nsmutablestring holds appended (or deleted, or whatever).
conceptually, cocoa has fair number of classes immutable. nsstring, nsarray, nsdictionary, etc. these not intended use building things incrementally, loop does.
when building things, or making things need inherently mutable (changeable, modifiable), should use mutable subclass: nsmutablestring, nsmutablearray, etc.
read string programming guide how use strings sensibly.
read cocoa fundamentals guide, explains cocoa's overall design, mutable/immutable relationships.
edit
seems apparent you're not following steps in book or tutorial. can't tell whether you're using book or online reference things up, i'd guess not. poor way learn.
reason books , tutorials exist because present information in sequence, , show correct things @ each step of sequence. if don't see sequence, or aren't looking things in reference, you'll never learn there simpler approaches solving problems (such mutable subclasses). favor, , follow actual book, in sequence, without skipping around. @ minimum, go 1 of books have , nsmutablestring in index.
Forums iPhone, iPad, and iPod Touch iOS Programming
- iPhone
- Mac OS & System Software
- iPad
- Apple Watch
- Notebooks
- iTunes
- Apple ID
- iCloud
- Desktop Computers
- Apple Music
- Professional Applications
- iPod
- iWork
- Apple TV
- iLife
- Wireless
Comments
Post a Comment