cocoa touch - Usage of NSString and NSMutableString objects in Objective C -


i need use bunch of string variables throughout program. reassign of them quite often, while others stuck same value during execution. what's best practice here?

in first case, variables should nsmutablestring , should cast them nsstring (using copy method) whenever need arguments of functions require nsstring objects. right?

when reassign them other constant values, shouldn't have dispose previous content, right?

as nsstring objects, if need assign new value them, guess should deallocate them, allocate them again, , assign new value. correct?

unless you're modifying string, shouldn't use nsmutablestring. you're reassigning whole string new value, stay regular nsstring. use autoreleased versions, because that'll more efficient alloc/init/release time. reassign strings constants if know they'll assigned to.

in first case, variables should nsmutablestring , should cast them nsstring (using copy method) whenever need arguments of functions require nsstring objects. right?

well, way, really inefficient. remember inheritance—an nsmutablestring is nsstring, new stuff tacked on. simple cast trick:

nsstring *string = (nsstring *)amutablestring; 

even better though, don't have that. because of inheritance, can directly pass in mutable string wherever regular string required, no casting required. that's beauty of inheritance.

when reassign them other constant values, shouldn't have dispose previous content, right

for neither mutable or immutable strings. old values overwritten in memory—nothing dispose of there. far memory management goes, it's not efficient literally creating new strings time. reassign them. never need alloc/init 1 string more once, , single init should balanced single release.

addendum: when should use mutable? mutable string should used when physically changing value of existing string, without discarding old value. examples might include adding character beginning or end, or changing character in middle. mutable string, can "in place"—you'll modify existing string. contrast, immutable string, once value set, cannot change that value. nsstring has methods such stringbyappendingstring:, add string existing one—but returns new string. behind scenes, nsstring has copied old string new (larger) memory location, added argument, , returned new string. copying lot less efficient (relatively speaking, or if have lot).

of course, there's nothing stopping physically assigning 1 string another. old values overwritten. nsstrings, including @"string constants", autoreleased. if creating new string , decide alloc/init, can assign value without consequence:

mystring = anotherstring; mystring = mytextfield.text; 

you can both mutable , immutable strings. main takeaway should use mutable when changing string itself. can change variable both mutable , immutable strings without compiler or runtime issues (short of memory management, of autoreleased anyway).


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -