iosobjective-cnsstringcfstring

Are UniChar (from CFString) and unichar (from NSString) the same?


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..


Solution

  • 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.