2012-01-03 22 views
3

我一直在用iOS 5.0的新NSLinguisticTagger在Xcode 4.2中摆弄。我用这个函数的目标是获取一个地址簿记录,然后将一个复合名称作为一个NSString来分发,就像ABRecordCopyCompositeName所做的一样,但是考虑到东亚语言和匈牙利语的命名顺序(最后一个而不是最后一个) )。这里的功能:NSLinguisticTagger内存泄漏

NSString *text = [self getLocalizedFullNameOfRecord:[contacts objectAtIndex:indexPath.section]; 


- (NSString *) getLocalizedFullNameOfRecord:(ABRecordRef) person 
{ 
    NSString *firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty); 
    NSString *middleName = ABRecordCopyValue(person, kABPersonMiddleNameProperty); 
    NSString *lastName = ABRecordCopyValue(person, kABPersonLastNameProperty); 
    NSString *prefix = ABRecordCopyValue(person, kABPersonPrefixProperty); 
    NSString *suffix = ABRecordCopyValue(person, kABPersonSuffixProperty); 
    NSString *fullName = @""; 

    __block BOOL Asian; 
    // Apologies to all Hungarians who aren't actually Asian 
    __block NSArray *asianLanguages = [NSArray arrayWithObjects:@"zh-Hant", @"zh-Hans", @"ja", @"ko", @"hu", @"vi", nil]; 

    [firstName enumerateLinguisticTagsInRange:NSMakeRange(0, firstName.length) scheme: NSLinguisticTagSchemeLanguage options: NSLinguisticTaggerOmitWhitespace orthography: nil usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop){ 
     if ([asianLanguages containsObject:tag]) 
      Asian = YES; 
     else 
      Asian = NO; 
    }]; 

    if(prefix) 
     fullName = [fullName stringByAppendingFormat:@"%@ ", prefix]; 
    if(Asian && lastName) 
     fullName = [fullName stringByAppendingFormat:@"%@ ", lastName]; 
    else if(firstName) 
     fullName = [fullName stringByAppendingFormat:@"%@ ", firstName]; 
    if(middleName) 
     fullName = [fullName stringByAppendingFormat:@"%@ ", middleName]; 
    if(Asian && firstName) 
     fullName = [fullName stringByAppendingFormat:@"%@ ", firstName]; 
    else if(lastName) 
     fullName = [fullName stringByAppendingFormat:@"%@ ", lastName]; 
    if(suffix) 
     fullName = [fullName stringByAppendingFormat:@"%@", suffix]; 

    [firstName release]; 
    [middleName release]; 
    [lastName release]; 
    [prefix release]; 
    [suffix release]; 

    return fullName; 
} 

仪器告诉我,我一定泄漏16至32个字节具有这种功能的每次迭代,在enumerateLinguisticTagger(而不是块部分,显然)。由于NSLinguisticTagger的在线资源仅限于其类参考和单个教程,因此我不知道在哪里以及如何开始寻找泄漏。

请帮忙吗?

+0

对于有用的代码片段+1。我正在看着这个(http://www.w3.org/International/questions/qa-personal-names),想知道我该怎么做,嘿,快到了,可怜的救援:-) – 2012-12-13 15:55:45

回答

0

我有同样的问题。在我的情况下,当字符串有换行符(\ n或\ r)时发生泄漏。