NSMutableString 帮助 Objective C
我目前有以下代码:
-(void) inputNumber:(int)number {
NSString *str;
str = [NSString stringWithFormat:@"%d", number];
[strVal appendString:str];
txtShowNum.text = strVal;
}
我已经定义了 NSMutableString *strVal;之前在我的代码中。
当上面的函数执行时,该字段保持空白,但如果我要使用:
txtShowNum.text = str;
我得到了我想要的值,但我显然需要连接该值。
谁能解释一下这一点。
谢谢
I currently have the following code:
-(void) inputNumber:(int)number {
NSString *str;
str = [NSString stringWithFormat:@"%d", number];
[strVal appendString:str];
txtShowNum.text = strVal;
}
I have already defined NSMutableString *strVal; before in my code.
When the function above executes the field remains blank, but if i were to use:
txtShowNum.text = str;
I get the value that I'm meant to but I obviously need the value concatenated.
Can anyone shed some light on this.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还应该在调用此方法之前的代码中分配一个
NSMutableString
。例如这样:
但是这里存在一些内存问题。您最好将
strVal
设为保留属性,然后执行以下操作:You should allocate an
NSMutableString
as well in your code, somewhere before this method is called.E.g. like this:
There are however some memory issues here. You'd better make
strVal
a retained property and then do:您的
strVal
很可能是nil
,因此您在 nil 上调用appendString:
,这实际上什么都不做,并将文本字段设置为 nil 将删除其内容。Your
strVal
is most probablynil
, so you callappendString:
on nil which essentially does nothing at all, and setting the text field to nil will erase its contents.如果您刚刚将其声明为成员变量,请确保在某处初始化它:
NSMutableString* strVal = @"";
如果没有,您只是在 nil 对象上调用方法。
If you just declared it as a member variable, make sure you initialize it somewhere:
NSMutableString* strVal = @"";
If not, you are just calling a method on a nil object.