如何在不使用componentsSeparatedByString方法的情况下在目标c中打印NSString的反向?

use*_*279 1 objective-c nsstring

我想创建一个方法,给出string.suppose的反向我在方法中传递NSString"欢迎使用Objective C",该方法返回字符串的反向,如"C Objective to Welcome"而不是"C evitcejbO ot emocleW"而不使用componentsSeparatedByString方法.是否可以使用Objective c ..?请帮忙.

Fog*_*ter 6

您可以按单词枚举字符串.

NSString *string = @"Welcome to Objective-C!";

NSMutableArray *words = [NSMutableArray array];

[string enumerateLinguisticTagsInRange:NSMakeRange(0, [string length])
                                scheme:NSLinguisticTagSchemeTokenType
                               options:0
                           orthography:nil
                            usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop) {
                                [array addObject:[string substringWithRange:tokenRange]];
                            }];

NSMutableString *reverseString = [[NSMutableString alloc] init];

for (NSString *word in [words reverseObjectEnumerator]){
    [reverse appendString:word];
}

NSLog(@"%@", reverseString);
Run Code Online (Sandbox Code Playgroud)

这将打印...

"!C-Objective to Welcome"
Run Code Online (Sandbox Code Playgroud)

您可以更改选项以省略空格和内容......

  • 不,这是一个完全不同的功能.componentsSeparatedByString将以某些语言中断.例如,日语不使用单词分隔符的空格,因此componentSeparatedByString:@""不起作用.基本上,是的,你是在做同样的事情.但是,用于反转字符串的单词顺序的任何函数都是如此.此外,cSBS不会看到标点符号,即.Objetcive-C仍然是Objective-C而不是C-Objective. (3认同)