开发者

attempt to mutate immutable object randomly thrown

One part of the program takes text from a uitextfield, copies it to a mutable string and then performs

     sharedManager.ce_name=name.text


    [sharedManager.ce_name replaceOccurrencesOfString:@" " withString:@"%20"
     options:NSLiteralSearch range:NSMakeRange(0, [sharedManager.ce_name length])];

At this point it always gave me "attempt to mutate immutable object" - it was not random

The first time I got this error I changed it to

    shar开发者_运维知识库edManager.ce_name=(NSMutableString *)name.text

This STILL gave me the attempt to mutate immutable object error, but it would occur randomly - weird right?

I then changed it to

    NSMutableString *mutable_name = [NSMutableString stringWithString:name.text];

    sharedManager.ce_name=mutable_name;

It has yet to fail on me doing it this way but I am convinced that I have not found the solution.

my questions:

1) Could that fact that it was doing it randomly after the first fix indicate I have some deep seated memory management problem?

2) Why didn't the C-style cast fix it?

3) Will my current fix work?

Thanks for your time :)


The problem here is the way in which you're using casting. When you cast a pointer, it just starts treating the memory at that location as though it were a block of data representing that type.

So if I've got a pointer to a Car class: Car* mycar;, and I cast it to a Person object: (Person*)mycar;, the program will try to access the memory there as though it was pointing at a Person object. However, a car is not a person, except in an old TV sitcom, so when it tries to access members on this or call functions, it's going to try to go to a memory location that contains something other than what it's expecting, and undefined things happen (generally, crashing).

NSMutableString and NSString are incompatible in this regard, and so casting from one to the other will result in terrible times. Your fix ([NSMutableString stringWithString:]) is the correct solution.


  1. If it was doing it randomly, it means that name.text was sometimes a mutable string and some times an immutable string.

  2. Casting between objects like that doesn't change the class of the object. It won't make your immutable object mutable.

  3. That "fix" is probably the best way to do it (at least from what I can see in the code you are showing)


Without seeing at least the declarations of the variables involved, it's difficult to say for certain, but your final solution, creating a new mutable string is likely the correct fix. As for your questions,

  1. Not memory management per se, but it was probably overwriting something it shouldn't have somewhere.
  2. Casts cannot change the fundamental type of an object. You had (presumably) an NSString and all the casting in the world cannot make it into an NSMutableString.
  3. Like I said, probably, but we'd need to see more code to make sure. It's certainly a much better fix.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜