views:

268

answers:

1

If I set a mutable string's value to a value from an array, and use the following code to manipulate it:

NSMutableString *theCountry = [listItems objectAtIndex:3];
theCountry = [theCountry stringByReplacingOccurrencesOfString:@"\"" withString:@""];

I receive the warning "warning: assignment from distinct Objective-C type" after the second line of the above code. If I do not have "theCountry =" before the method call, the warning goes away, but the string does not get manipulated...

+5  A: 

The stringByReplacingOccurrencesOfString:withString: method is declared to return an NSString*, not an NSMutableString*. Basically, in the assignment, you're assigning an NSString* to a variable of type NSMutableString* which is not necessarily safe (note that NSMutableString inherits NSString, not the other way around).

Mehrdad Afshari
I'll add that whenever you get this warning it means you are assigning an object of type X into a variable declared for type Y. If you did this intentionally then you should have casted the object, and if you did this unintentionally then you should fix it :-)
Nir Levy
Thanks, I just changed NSMutableString to NSString and it works fine now. No warnings and app still works correctly.
rson