尝试改变随机抛出的不可变对象

发布于 2024-12-02 01:46:54 字数 786 浏览 5 评论 0原文

程序的一部分从 uitextfield 中获取文本,将其复制到可变字符串,然后执行

     sharedManager.ce_name=name.text


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

此时它总是给我“尝试改变不可变对象” - 它不是随机的

我第一次收到此错误时将其更改为

    sharedManager.ce_name=(NSMutableString *)name.text

这仍然给了我尝试改变不可变对象的错误,但它会随机发生 - 很奇怪吧?

然后我将其更改为

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

    sharedManager.ce_name=mutable_name;

“这样做还没有失败”,但我确信我还没有找到解决方案。

我的问题:

1)第一次修复后它随机执行的事实是否表明我有一些根深蒂固的内存管理问题?

2)为什么C型演员没有修复它?

3) 我当前的修复有效吗?

感谢您的宝贵时间:)

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

    sharedManager.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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疯到世界奔溃 2024-12-09 01:46:54

这里的问题是您使用强制转换的方式。当您转换指针时,它只是开始将该位置的内存视为表示该类型的数据块。

因此,如果我有一个指向 Car 类的指针:Car* mycar;,并将其转换为 Person 对象:(Person*)mycar;,程序将尝试访问那里的内存,就好像它指向一个人对象。然而,汽车不是人,除了在旧的电视情景喜剧中,因此当它尝试访问此功能或呼叫功能上的成员时,它将尝试前往包含与预期不同的内容且未定义的内容的内存位置事情发生了(通常是崩溃)。

NSMutableString 和 NSString 在这方面是不兼容的,因此从一个到另一个的转换将导致可怕的后果。您的修复 ([NSMutableString stringWithString:]) 是正确的解决方案。

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.

浅忆 2024-12-09 01:46:54
  1. 如果它是随机执行的,则意味着 name.text 有时是可变字符串,有时是不可变字符串。

  2. 像这样的对象之间的转换不会改变对象的类。它不会使您的不可变对象变得可变。

  3. 那个“修复”可能是最好的方法(至少从我在您显示的代码中看到的)

  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)

尹雨沫 2024-12-09 01:46:54

至少在没有看到所涉及变量的声明的情况下,很难确定,但您的最终解决方案,创建一个新的可变字符串可能是正确的解决方案。至于你的问题,

  1. 本身不是内存管理,但它可能覆盖了一些不应该存在的东西。
  2. 强制转换不能改变对象的基本类型。你(大概)有一个 NSString 并且世界上所有的转换都无法将其变成 NSMutableString。
  3. 就像我说的,可能是这样,但我们需要查看更多代码来确定。这当然是一个更好的解决方案。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文