YoungSeung-Seo
1/16/2018 - 6:55 AM

[iOS] 초성 비교

#pragma mark - 초성 뽑아오기.

- (NSComparisonResult) compare:(NSString *)source withChoungString:(NSString *)search
{
    return [[self stringChosung:source] compare:search];
}


- (NSString*) stringChosung:(NSString *)source
{
    NSMutableString *result = [NSMutableString string];
    
    for (NSUInteger i = 0; i < [source length]; i++) {
        NSInteger unicodeChar = [source characterAtIndex:i];
        
        // 한글인지 검색
        if ( HANGUL_START_CODE <= unicodeChar && unicodeChar <= HANGUL_END_CODE )
        {
            NSInteger chosungIndex  = (NSInteger)((unicodeChar - HANGUL_START_CODE) / (28*21));
            // 중성, 종성은 현재 필요없다.
            //            NSInteger jungsungIndex = (NSInteger)((unicodeChar - HANGUL_START_CODE) % (28*21) / 28);
            //            NSInteger jongsungIndex = (NSInteger)((unicodeChar - HANGUL_START_CODE) % 28);
            
            [result appendFormat:@"%@", [self.chosung objectAtIndex:chosungIndex]];
        }
        else
        {
            NSRange rang = NSMakeRange(i, 1);
            [result appendFormat:@"%@", [source substringWithRange:rang]];
        }
        
    }
    
    return result;
}