unichar and NSString - how interchangeable are these?(unichar 和 NSString - 它们的互换性如何?)
问题描述
有一个 NSString
方法 -characterAtIndex:
返回一个 unichar.
There is an NSString
method -characterAtIndex:
which returns an unichar.
- (unichar)characterAtIndex:(NSUInteger)index
我想知道这个显然不是 NSString
的 unichar 是否可以转换为 NSString
或馈送到 NSString
进行比较目的?
I wonder if this unichar, which obviously is not an NSString
, can be converted into an NSString
or feeded to an NSString
for comparing purposes?
而且:NSString
内部是否只包含 unichar 项的数组?
And: Does an NSString
internally just consist of an array of unichar items?
推荐答案
你有两个选择:第一个是一个类别,在 NSString 中添加一个 stringWithUnichar 方法:
You have two options: the first is a category adding a stringWithUnichar method to NSString:
@interface NSString (MNNSStringWithUnichar)
+ (NSString *) stringWithUnichar: (unichar) value;
@end
@implementation NSString (MNNSStringWithUnichar)
+ (NSString *) stringWithUnichar:(unichar) value {
NSString *str = [NSString stringWithFormat: @"%C", value];
return str;
}
@end
然后用
NSString *otherString = @"TestString";
NSString *str = [NSString stringWithUnichar:[otherString characterAtIndex:1]];
或者你可以直接这样:
NSString *str = [NSString stringWithFormat:@"%C",[otherString characterAtIndex:1]];
如果你想重复使用它,或者如果你只需要做一次,请选择类别,使用 stringWithFormat 示例!
Choose the category if you want to use it repeatedly, or if you only have to do it once, use the stringWithFormat example!!
不确定 NSString 的内部结构,但我猜它可能是包装了一个
Not to sure about the internals of NSString, but I'd guess it is probably wrapping a
unichar *
或一个 unichars 数组(如 C char *)
or an array of unichars (like a C char *)
unichar 只是一个 16 位的值,用于存储字符.与只有 8 位的 unsigned char 不同,它可以容纳 0-255 以上,因此它也可以容纳 16 位的 unicode 字符.NSString 是一个(集群)类[es],其中包含一个 unichars 数组
A unichar is simply a 16bit value that is used to store a character. Unlike an unsigned char which is only 8 bits, it can hold more than 0-255, so it can hold unicode characters as well, which are 16bits. A NSString is a (cluster of) class[es] that contains an array of unichars
编辑
这里有几篇关于人们认为 NSString 是什么的文章:
Here are a few articles about what people think an NSString is:
http://cocoawithlove.com/2008/08/string-philosophies-char-arrays.html
http://www.reddit.com/r/programming/comments/6v1yw/string_philosophies_char_arrays_stdstring_and/
希望对您有所帮助!
这篇关于unichar 和 NSString - 它们的互换性如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:unichar 和 NSString - 它们的互换性如何?


- Android viewpager检测滑动超出范围 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01