I'm in the middle of doing some string manipulation using high-level Cocoa features like NSString
and NSData
as opposed to digging down to C-level things like working on arrays of char
s.
For the love of it, +[NSString stringWithUTF8String:]
sometimes returns nil
on a perfectly good string that was created with -[NSString UTF8String]
in the first place. One would assume that this happens when the input is malformed. Here is an example of the input that fails, in hex:
55 6B 66 51 35 59 4A 5C 6A 60 40 33 5F 45 58 60 9D 47 3F 6E 5E
60 59 34 58 68 41 4B 61 4E 3F 41 46 00
and ASCII:
UkfQ5YJ\j`@3_EX`G?n^`Y4XhAKaN?AF
This is a randomly generated string, to test my subroutine.
char * buffer = [randomNSString UTF8String];
// .... doing things .... in the end, buffer is the same as before
NSString * result = [NSString stringWithUTF8String:buffer];
// yields nil
Edit: Just in case somebody didn't grasp the implicit question, here it is in -v mode:
Why does [NSString stringWithUTF8String:] sometimes return nil
on a perfectly formed UTF8-String?