NSString's initWithData:encoding: return type issue
A "type mismatch" from the compiler when you try to use stringReply in another location has nothing to do with the object being returned from initWithData:encoding: and everything to do with where stringReply is subsequently being used.
For example, if you are getting a type mismatch when you do this:
SomeClass *someObject;
NSString *stringReply;
stringReply = [[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding];
[someObject methodWithString:stringReply];
The problem is not that initWithData:encoding: is returning the wrong type but one of:
- methodWithString: doesn't actually take an NSString*
- methodWithString: is not properly declared/included before being used
The second option can often happen if SomeClass is forward declared in a header file (as @class SomeClass) but never subsequently included in the implementation file.
Roman
Updated on December 04, 2020Comments
-
Roman over 3 years
I have a couple of lines of trivial code such as the following:
NSData *dataReply; NSString *stringReply; dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; stringReply = [[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding];
The problem here is, initWithData:encoding: does not return an instance of NSString as the documentation claims it does. I've tried doing an explicit cast using (NSString *) as well without much luck. This is giving me compiler warnings when I try to pass the stringReply to a method I've written with type mismatches. Given I treat all warnings as errors, I'd really like to understand what stringReply is being returned as and how I can enforce it to be of type NSString.