是否可以增加一个字母,即A + 1 = B 在 Objective-C 中?

发布于 2024-10-11 13:59:09 字数 299 浏览 7 评论 0原文

我习惯在 C 或 C++ 中这样做,即:

myChar++;

应该增加一个字母。

我试图在 Objective-C 中做同样的事情,除了我有一个 NSString 开始(NSString 总是只有一个字母)。我尝试过将 NSString 转换为 char *,但此方法已被弃用,并且其他实现此目的的方法似乎不起作用。

我应该如何将 NSString 转换为 char * - 或者,有没有办法在 Objective-C 中增加字符而不需要 char * ?

谢谢 :)

I am used to doing this in C or C++, ie:

myChar++;

should increment a letter.

I am trying to do the same in Objective-C, except that I have a NSString to start off with (the NSString is always just one letter). I have tried converting the NSString to a char *, but this method is deprecated and other ways of achieving this don't seem to work.

How should I convert an NSString to a char * - or, is there a way to increment a character in objective-c without needing a char * somehow?

Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以歌曲疗慰 2024-10-18 13:59:09
// Get the first character as a UTF-16 (2-byte) character:
unichar c = [string characterAtIndex:0];
// Increment as usual:
c++;
// And to turn it into a 1-character string again:
[NSString stringWithCharacters:&c length:1];

当然,这假设递增 Unicode 字符是有意义的,这对于 ASCII 范围字符有效,但对于其他字符可能无效。

// Get the first character as a UTF-16 (2-byte) character:
unichar c = [string characterAtIndex:0];
// Increment as usual:
c++;
// And to turn it into a 1-character string again:
[NSString stringWithCharacters:&c length:1];

Of course, this assumes incrementing a Unicode character makes sense, which does for ASCII-range characters but probably not for others.

坦然微笑 2024-10-18 13:59:09

怎么样?

- (unichar)characterAtIndex:(NSUInteger)index;

NSString 的效果

How about NSString's

- (unichar)characterAtIndex:(NSUInteger)index;

Would that work?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文