NSMutableString stringWithString 导致崩溃
ten.textValue = [[NSMutableString alloc]init];
ten.textValue = [NSMutableString stringWithString:textField.text];
我在第二行崩溃了。ten.textValue 是 NSMutableString
。
ten.textValue = [[NSMutableString alloc]init];
ten.textValue = [NSMutableString stringWithString:textField.text];
I am getting crash at second line.ten.textValue is NSMutableString
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能是因为
UITextField
的text
属性 默认为nil
,并将nil
传递给[NSMutableString stringWithString:nil]
导致崩溃。当您传递要复制的
text
时,您需要确保它不是nil
,例如:您还应该删除第一行 - 它没有任何作用,因为分配的值会立即被覆盖。
It is probably because the
text
property ofUITextField
isnil
by default, and passingnil
to[NSMutableString stringWithString:nil]
causes a crash.You need to make sure the
text
is notnil
when you pass it to be copied, for example like this:You should also eliminate the first line - it serves no purpose, because the allocated and assigned value gets overwritten immediately.
当您创建
ten.textValue = [[NSMutableString alloc]init];
时,您正在创建一个您拥有的对象。当您尝试在下一行中向其中添加字符串时,您正在创建一个自动释放的字符串。这让编译器感到困惑,它报告“挂起 - 这已经是一个已分配的、拥有的对象”。
反而:
When you create your
ten.textValue = [[NSMutableString alloc]init];
you are creating an object that you own.When you try to add a string to it in the next line, you are creating an autoreleased string. This is confusing the compiler, which is reporting "hang on - this is an allocated, owned object already".
Instead: