2011-12-29 73 views
1

我想通过这个按钮可以从我的标签单独删号 NSMutableString *str=(NSMutableString *)label.text;的Objective-C ....通过按钮

str=[str replaceCharacterInRange:NSMakeRange([str length]-1,1) withString:@""];

错误删除的数字从我的标签..... "Void value not ignored as it ought to be"

回答

2

您不能仅将NSString作为NSMutableString进行投射,并期望它是可变的。在改变它之前,你需要创建一个可变字符串。

NSMutableString *mutableString = [label.text mutableCopy]; 
[mutableString replaceCharactersInRange:NSMakeRange([mutableString length] - 1, 1) withString:@""]; 
+0

感谢...但要注意:按钮的label.text返回标签.... – CR7 2011-12-29 21:51:31

+0

请澄清你的问题。我无法理解你可能会遇到的其他问题。 – 2011-12-29 23:35:19

1

replaceCharacterInRange:withString:返回void,因为它是一个修改字符串的可变操作。

要解决您的问题,您需要知道的第一件事是,您只需将它转换为NSMutableString即可使用,您需要使用mutableCopy

NSMutableString *str= [label.text mutableCopy]; 

//Now the next thing do not assign str 
[str replaceCharacterInRange:NSMakeRange([str length]-1,1) withString:@""]; 

... 
//And finally when you are done if you are not using ARC 
///then you need to release the string since you called `mutableCopy`. 
[str release]; 
+0

非常感谢..但按钮不响应的行动:D – CR7 2011-12-29 21:43:11

0

使用deleteCharactersInRange:

[str deleteCharacterInRange:NSMakeRange([str length]-1,1) ])]