2

I have a NSMutableString and I am trying to use appendString method. Though it works fine but every time I use it, I receive a warning NSString may not respond to append string. This is my code :

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

    [soapResults appendString: string];        //soapResults is an NSMutableString in .h
}  

I know I am getting this warning because of NSString string but how to remove this warning?

halfer
  • 19,824
  • 17
  • 99
  • 186
Nitish
  • 13,845
  • 28
  • 135
  • 263

3 Answers3

3
self.soapResults=(NSMutableString *)[self.soapResults stringByAppendingString:string];

or

soapResults=(NSMutableString *)[soapResults stringByAppendingString:string];

OR

self.soapResults=(NSMutableString *)[[self.soapResults stringByAppendingString:string] mutableCopy];

    //    or

soapResults=(NSMutableString *)[[soapResults stringByAppendingString:string] mutableCopy];
3

If you are sure soapResults is actually an NSMutableString, and only have a warning, then simply cast it before calling appendString: to it :

[(NSMutableString*)soapResults appendString: string]; 

If you declared soapResults as an NSMutableString but in fact affected an NSString to it, then you probably don't have a warning, but will have a runtime error on execution.

If soapResults is an NSString (either because you declared it so or because you affected an NSString to it), you may instead create a new NSString by appending string to it, then create a mutableCopy from it... but I don't recommend this as it will generate useless allocations (and NSMutableString is better to avoid this)

AliSoftware
  • 32,623
  • 6
  • 82
  • 77
  • Thanks a lot. But as AppleVijay posted the correct answer earlier I must accept his answer :) – Nitish Sep 06 '11 at 11:19
2

Simply, at this point in the code soapResults is not a NSMutableString. Perhaps somewhere else in the code it is inadvertently replaced with a NSString.

As an aside, NSString is really an NSMutableString code-wise even though NSMutableString is a subclass of NSString. This might help explain what is happening in the code.

Still, casting an NSString to an NSMutableString is not a good idea, what really needs to be done is find where it is becoming an NSString and fix that.

zaph
  • 111,848
  • 21
  • 189
  • 228