views:

1073

answers:

2

I've got an RSS parser method and I need to remove whitespace and other nonsense from my extracted html summary. I've got a NSMutableString type 'currentSummary'. When I call:

currentSummary = [currentSummary 
        stringByReplacingOccurrencesOfString:@"\n" withString:@""];

Xcode tells me "warning: assignment from distinct Objective-C type"

What's wrong with this?

+9  A: 

If currentSummary is already a NSMutableString you shouldn't attempt to assign a regular NSString (the result of stringByReplacingOccurrencesOfString:withString:) to it.

Instead use the mutable equivalent replaceOccurrencesOfString:withString:options:range:, or add a call to mutableCopy before the assignment:

// Either
[currentSummary replaceOccurencesOfString:@"\n" 
                               withString:@"" 
                                  options:NULL
                                    range:NSMakeRange(0, [receiver length])];

// Or
currentSummary = [[currentSummary stringByReplacingOccurrencesOfString:@"\n"
                                                            withString:@""]
                  mutableCopy];
Tim
Thank you! Worked great.
isaaclimdc
A: 

That usually means you dropped the asterisks in the definition of (in this case) currentSummary.

So you most likely have:

NSMutableString currentSummary;

when you need:

NSMutableString *currentSummary;

In the first case, since Objective-C classes are defined in type structures, the complier thinks your trying to assign a NSString to a struct.

I make this typo on a distressingly regular basis.

TechZen