我一直在使用iOS 5.0和新的NSLinguisticTagger摆弄Xcode 4.2。我使用此函数的目的是接收一个地址簿记录,然后将一个复合名称作为NSString吐出,类似于ABRecordCopyCompositeName所做的,但考虑到东亚语言和匈牙利语的命名顺序(最后一个而不是最后一个) )。这是功能:
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;
}
Instruments告诉我,在enumerateLinguisticTagger(而不是块部分,显然)上,每次迭代此函数时,我会泄漏大约16到32个字节。由于NSLinguisticTagger的在线资源仅限于其类引用和单个教程,我不知道在哪里以及如何开始寻找泄漏。
请帮忙吗?
答案 0 :(得分:0)
我遇到了同样的问题。就我而言,当字符串有换行符(\ n或\ r)时会发生泄漏。