NSString
does not have a stringWithBytes:length:encoding:
class method, but you can use
NSString* word2 = [[NSString alloc] initWithBytes:word
length:nChars
encoding:NSUTF8StringEncoding];
Note however, that scanf()
returns the number of scanned items and
not the number of scanned characters. So nChars
will contain 1
and not the string length, so you should set nChars = strlen(word)
instead.
A simpler alternative is (as also mentioned in one answer to the linked question)
NSString* word2 = [NSString stringWithUTF8String:word];