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 - 它们的互换性如何?
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01