我认为你向后陈述了问题,但是为了测试NSString中的每个字符是否都是唯一的,我认为以下内容应该有效.可能有一些有趣的unicode边缘情况不起作用,其中相同的字形显示为不同的代码点.
@interface NSString(_uniqueChars)
-(BOOL) isEveryCharacterUnique;
@end
@implementation NSString(_uniqueChars)
-(BOOL) isEveryCharacterUnique
{
NSMutableSet *set = [NSMutableSet setWithCapacity:self.length];
for ( NSUInteger i = 0; i < self.length; ++i )
{
unichar c = [self characterAtIndex:i];
[set addObject:[NSNumber numberWithUnsignedShort:c]];
}
return (set.count == self.length);
}
@end
Run Code Online (Sandbox Code Playgroud)