I was triggered to this question because I used a category method on NSString from this answer to count the number of occurrences for a specific character:

https://stackoverflow.com/a/15947190/472599

This method is very fast, by using the special CFString methods for enumerating the contents of the string. It needs a UniChar as a parameter. To obtain a UniChar, I used:

unichar semicolon = [@";" characterAtIndex: 0];

Because I could not find anything that gave me a UniChar in NSString.

The compiler does not complain about this (passing the unichar in place of a UniChar

The definitions of the types are:

// in MacTypes.h
typedef UInt16                          UniChar;
// and:
typedef unsigned short                  UInt16;

// in NSString.h:
typedef unsigned short                  unichar;

So these types seem to be the same for now (iOS 7.1). But can we expect this to be the case forever? Having seen that NSInteger changed from int to long when moving to 64 bit, I want to be sure..

有帮助吗?

解决方案 2

Are they the same - yes they are. Will they be the same forever - who knows. Making them different will break a lot of code, that's for sure.

其他提示

Just to clarify: Both "UniChar" and "unichar" are not Unicode characters. They are UTF-16 components. Just like single bytes are UTF-8 components, UniChar and unichar are UTF-16 components. A single Unicode character consists of one to four UTF-8 components, or one or two UTF-16 components.

For ASCII characters, you can just write

unichar semicolon = ';';
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top