Just use NSMutableString
; mutability is why it exists. ;)
NSString
is constant. Not just "wraps a const char *
" constant, but "no, really, this thing is immutable and the storage details are entirely opaque to you".
In fact, an __NSCFConstantString
isn't even stored on the heap at all; there will be no malloc
d chunk of memory you can muck with. Such strings are generated by the compiler and the linker lays them down in a chunk of memory that will be read in at runtime and stored on read-only pages of memory.
But not even the results of [NSString stringWithFormat:@"%d World", 42]
will have storage in a chunk of uniquely allocated malloc()
d memory just for the string buffer. Most likely (but maybe not -- it is an opaque implementation detail), the mechanism for constructing the formatted string will yield an object whose size will be the minimum instance size for whatever private subclass of NSString
(see class clusters) is most appropriate + however many bytes are needed to store the string data itself.
While you could potentially find the actual bytes in memory and muck with them directly, that would be a gross violation of encapsulation and utterly useless in a real program.
Note that the isa
you are mucking with is an instance variable of NSObject
. It is a pointer to the Class object of the instance. It is not necessarily constant, but you really shouldn't muck with it, either.
For more information on the isa
and why you are seeing that particular crash when you stuck an NSString
instance into the isa
slot, my answer to this question may be helpful (maybe):
BTW: Love the question -- while you are going down a path that is completely at odds with OO programming and the Foundation, mucking about with the innards and wildly breaking things is a fantastic way to learn! Don't let the down voters get you...well...down, if they should appear.