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:@""];
The
stringByReplacingOccurrencesOfStringis a method of NSString, and the return value is also a NSString. So you can’t assign the return value to aNSMutableStringvariable.Instead you can use the method
replaceOccurrencesOfString:withString:options:range:fromNSMutableString:But, for the consideration of performance, I think use NSString and its methodstringByReplacingOccurrencesOfStringis 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.