2012-07-28 57 views
0

下面显示的代码的目的是从文本字段中取出一个字符串,并删除逗号和左括号,以准备转换为浮点的内容。例如,它需要数1,234,567,并将其更改为1234567使用NSMutableString和stringByReplacingOccurrencesOfString时不兼容的指针类型错误

的代码片段工作,但返回一个错误信息“不兼容的指针类型从字符串分配给的NSMutableString *”

当我用它代替的NSString NSMutableString,我没有得到任何错误,但返回的值是一个空字符串。

使用的NSMutableString的方法,这是什么问题,我怎样才能改变这种消除“不兼容的指针类型”错误

NSMutableString *cleanedDataCellTest = [[NSMutableString alloc] initWithString:datacellR2C2.text]; 
cleanedDataCellTest = [cleanedDataCellTest stringByReplacingOccurrencesOfString:@"," withString:@""]; 
cleanedDataCellTest = [cleanedDataCellTest stringByReplacingOccurrencesOfString:@")" withString:@""]; 
cleanedDataCellTest = [cleanedDataCellTest stringByReplacingOccurrencesOfString:@"(" withString:@"-"]; 
cleanedDataCellTest = [cleanedDataCellTest stringByReplacingOccurrencesOfString:@" " withString:@""]; 
+0

哪一行发出警告? – jrtc27 2012-07-29 00:21:06

回答

3

stringByReplacingOccurrencesOfString是一个NSString的方法,返回值也是一个NSString。因此,您无法将返回值分配给NSMutableString变量。

相反,你可以使用的方法replaceOccurrencesOfString:withString:options:range:NSMutableString

NSMutableString *cleanedDataCellTest = [[NSMutableString alloc] initWithString:@"1,234,567"]; 
[cleanedDataCellTest replaceOccurrencesOfString:@"," withString:@"" options:0 range:NSMakeRange(0, cleanedDataCellTest.length)]; 
[cleanedDataCellTest replaceOccurrencesOfString:@")" withString:@"" options:0 range:NSMakeRange(0, cleanedDataCellTest.length)]; 
[cleanedDataCellTest replaceOccurrencesOfString:@"(" withString:@"-" options:0 range:NSMakeRange(0, cleanedDataCellTest.length)]; 
[cleanedDataCellTest replaceOccurrencesOfString:@" " withString:@"" options:0 range:NSMakeRange(0, cleanedDataCellTest.length)]; 

但是,审议的表现,我认为使用NSString及其方法 stringByReplacingOccurrencesOfString更有效。

更新:对不起,昨天我没有测试“性能”。我刚刚做了一个测试,通过替换字符串中的某些东西(大约26KB),使用带有replaceOccurrencesOfString:withString:options:range:的NSMutableString有点效率更高

2
NSMutableString *cleanedDataCellTest = [[NSMutableString alloc] initWithString:datacellR2C2.text]; 
[cleanedDataCellTest replaceOccurrencesOfString:@"," withString:@"" options:0 range:NSMakeRange(0, cleanedDataCellTest.length)]; 

编号:https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/nsmutablestring_Class/Reference/Reference.html#//apple_ref/occ/instm/NSMutableString/replaceOccurrencesOfString:withString:options:range

1

切换到的NSString为我工作...:

NSString *cleanedDataCellTest = [[NSString alloc] initWithString:@"1,234,098"]; 
cleanedDataCellTest = [cleanedDataCellTest stringByReplacingOccurrencesOfString:@"," withString:@""]; 
cleanedDataCellTest = [cleanedDataCellTest stringByReplacingOccurrencesOfString:@")" withString:@""]; 
cleanedDataCellTest = [cleanedDataCellTest stringByReplacingOccurrencesOfString:@"(" withString:@"-"]; 
cleanedDataCellTest = [cleanedDataCellTest stringByReplacingOccurrencesOfString:@" " withString:@""]; 
NSLog(@"cleanedDataCellTest = %@", cleanedDataCellTest); 

显示:

cleanedDataCellTest = 1234098 
+0

似乎有一些[很好的解决方案](http://stackoverflow.com/questions/1129521/remove-all-but-numbers-from-nsstring)剥离除数字以外的所有内容。 – Jon 2012-07-29 00:19:06