简体   繁体   English

UniChar(来自CFString)和unichar(来自NSString)是一样的吗?

[英]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: 我被触发了这个问题因为我在这个答案中使用NSString上的类别方法来计算特定字符的出现次数:

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

This method is very fast, by using the special CFString methods for enumerating the contents of the string. 通过使用特殊的CFString方法枚举字符串的内容,此方法非常快。 It needs a UniChar as a parameter. 它需要一个UniChar作为参数。 To obtain a UniChar, I used: 为了获得UniChar,我使用了:

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

Because I could not find anything that gave me a UniChar in NSString. 因为我找不到任何能让我在NSString中使用UniChar的东西。

The compiler does not complain about this (passing the unichar in place of a UniChar 编译器不会抱怨这一点(通过unichar代替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). 所以这些类型现在似乎是相同的(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.. 看到NSInteger在转移到64位时从int更改为long,我想确定..

Just to clarify: Both "UniChar" and "unichar" are not Unicode characters. 只是为了澄清:两款“UNICHAR”和“单字符” 没有 Unicode字符。 They are UTF-16 components. 它们是UTF-16组件。 Just like single bytes are UTF-8 components, UniChar and unichar are UTF-16 components. 就像单个字节是UTF-8组件一样,UniChar和unichar是UTF-16组件。 A single Unicode character consists of one to four UTF-8 components, or one or two UTF-16 components. 单个Unicode字符由一到四个UTF-8组件或一个或两个UTF-16组件组成。

For ASCII characters, you can just write 对于ASCII字符,您只需编写即可

unichar semicolon = ';';

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. 让它们与众不同会破坏很多代码,这是肯定的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM