0

The purpose of the code shown below is to take a string from a textfield and remove commas and left parentheses, to prepare the contents for conversion to a float. For example, it takes the number 1,234,567 and changes it to 1234567.

The code snippet works, but returns an informational error "Incompatible pointer types assigning to NSMutableString * from String *"

When I use NSString instead of NSMutableString, I get no errors, but the returned value is an empty string.

Using the NSMutableString methodology, what is the problem and how can I change this to eliminate the 'Incompatible pointer type" error.

NSMutableString *cleanedDataCellTest = [[NSMutableString alloc] initWithString:datacellR2C2.text];
cleanedDataCellTest =  [cleanedDataCellTest stringByReplacingOccurrencesOfString:@"," withString:@""];
cleanedDataCellTest =  [cleanedDataCellTest stringByReplacingOccurrencesOfString:@")" withString:@""];
cleanedDataCellTest =  [cleanedDataCellTest stringByReplacingOccurrencesOfString:@"(" withString:@"-"];
cleanedDataCellTest =  [cleanedDataCellTest stringByReplacingOccurrencesOfString:@" " withString:@""];
K17
  • 697
  • 3
  • 8
  • 26

3 Answers3

3

The stringByReplacingOccurrencesOfString is a method of NSString, and the return value is also a NSString. So you can't assign the return value to a NSMutableString variable.

Instead you can use the method replaceOccurrencesOfString:withString:options:range: from 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)];

But, for the consideration of performance, I think use NSString and its method stringByReplacingOccurrencesOfString is more efficient.

UPDATE: Sorry, yesterday I didn't test the "performance". I did a test just now, by replacing some thing in a string (about 26KB), use NSMutableString with replaceOccurrencesOfString:withString:options:range: is a little bit more efficient.

Albert Zhang
  • 757
  • 1
  • 8
  • 19
2
NSMutableString *cleanedDataCellTest = [[NSMutableString alloc] initWithString:datacellR2C2.text];
[cleanedDataCellTest replaceOccurrencesOfString:@"," withString:@"" options:0 range:NSMakeRange(0, cleanedDataCellTest.length)];

Ref: 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:

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
jordanperry
  • 3,368
  • 2
  • 19
  • 13
1

Switching to NSString worked for me...:

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);

Shows:

cleanedDataCellTest = 1234098
Jon
  • 3,208
  • 1
  • 19
  • 30
  • There seem to be some [good solutions here](http://stackoverflow.com/questions/1129521/remove-all-but-numbers-from-nsstring) for stripping everything but numbers. – Jon Jul 29 '12 at 00:19