I’m getting a potentially leaked object error from the Static Analyzer for this line:
strCleanPhone = [[[[strPhone stringByReplacingOccurrencesOfString:@" " withString:@""]
stringByReplacingOccurrencesOfString:@"(" withString:@""]
stringByReplacingOccurrencesOfString:@")" withString:@""]
stringByReplacingOccurrencesOfString:@"-" withString:@""];
For one, is this the preferred way to strip non-numeric characters from a phone number string?
Two, can you possibly explain why this would be a leaked object?
The strings created by
stringByReplacingOccurrencesOfStringare autoreleased, so they aren’t leaked. If there’s a leak, it has to do withstrPhoneandstrCleanPhone.For example, if
strCleanPhoneis a @property with the retain option, and is currently not nil, then your code leaks it. To use the release/retain code that was generated by synthesize you have to use the property syntax:self.strCleanPhone = .... Using juststrCleanPhone = ...sets the instance variable and doesn’t release any object it was pointing to.