componentsSeparatedByString:@""(数组中的每个字符)

Bat*_*yne 0 cocoa objective-c nsstring nsarray

我想在一个中添加每个角色NSArray ("123" -> To NSArray with "1","2","3").我测试componentsSeparatedByString:@""componentsSeparatedByString:nil,但它不工作,谁能帮助我?

Mon*_*olo 6

根据您的需要,您可能希望按组合字符枚举字符串,这会考虑编码重音字符的不同方法.

这可能不是一个大问题,但如果你enumerateSubstringsInRange:options:usingBlock:至少使用它就会被处理掉.代码可能如下所示:

NSMutableArray *result;  

NSString *string = @"Genève, Zu\u0308rich, Bellinzona";
//                           ^
//      What humans know as: Zürich

result = [NSMutableArray array];
[string enumerateSubstringsInRange: NSMakeRange(0,string.length)
                           options: NSStringEnumerationByComposedCharacterSequences
                        usingBlock: ^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){
                            // If you want to see the way the string has been split
                            NSLog(@"%@", substring);
                            [result addObject: substring];
                        }
];
Run Code Online (Sandbox Code Playgroud)

请注意,"è"是单个字符,但"ü"已编码为组合字符.两者仍然被正确识别以用于循环.如果使用characterAtIndex:"ü"将分为两部分(u和¨),这很可能不是你想要的.