an ancient code I maintain originally had this:
NSUInteger readUnicode(NSMutableString *string, signed char const * buf, signed long int offset, size_t bufsize)
{
NSUInteger initLength = [string length];
signed char const * start = buf + offset;
NSLog (@"--------> start: %S", start);
[string appendFormat:@"%S", start];
return [string length] - initLength;
}
with XCode throwing a warning “Format specifies type ‘const unsigned short *’ but the argument has type ‘const signed char *’ and suggesting to change appendFormat:@”%S” to appendFormat:@”%s”
when I did change %S to %s I started getting only ONE character in return string, regardless of the input.
So instead I changed the code to:
NSUInteger readUnicode(NSMutableString *string, signed char const * buf, signed long int offset, size_t bufsize)
{
NSUInteger initLength = [string length];
const unsigned short * start = (const unsigned short * )buf + offset;
NSLog (@"--------> start: %S", start);
[string appendFormat:@"%S", start];
return [string length] - initLength;
}
… and now I seem to be getting all the characters and without the warning.
Is this the way to deal with the warning ? Or perhaps I could/should have done something else ? (Other than changing the type of the input buf)
Thank you.
Perhaps not exactly. If you work with wide strings, why don’t you declare your function to do so?
Then you don’t need the ugly cast, you won’t get the warning and your program wouldn’t invoke undefined behavior.