문제

I've got a custom C++ string class in my iOS app that is based on an array of wchar_t. I have a method to convert it to NSString that looks like this:

NSString *str = [[[NSString alloc]initWithBytes:(const void *)mArray
      length:sizeof(wchar_t) * mLength 
      encoding:NSUTF32LittleEndianStringEncoding]autorelease];
return str;

I've got a case where the input string contains some Chinese characters (specifically 新譯本), and initWithBytes returns nil. There is nothing in the documentation about the method returning nil; is this an error condition? Does anyone know what kind of thing might make the method kick back a nil result?

도움이 되었습니까?

해결책

It turns out that the wchar array that was passed in sometimes had UTF-16 characters in it, and UTF-16 encodes multibyte characters differently. And if initWithBytes gets invalid data, it returns nil.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top