我正在从 coreData 获取我的实体并将 fetchrequest 的结果保存到 NSMutableString 以进行文本到语音的转换。
self.ttsInboxCards = [[NSMutableString alloc] initWithString:@""];
[self.ttsInboxCards appendString:[[entitySetsCards valueForKey:@"cardTitle"] description]];
在 NSLog 中我得到了这个值:
F\U00fcr | schl\U00e4ge | zuh\U00f6ren
应该是:
Für | schläge | zuhören
我已经尝试了很多方法来获得正确的编码,例如:
stringWithUTF8String:
等等,但没有任何效果。
我怎样才能避免这个问题?
编辑 1:
我像这样从 coreData 获取值:
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"EntitySetsCardsInbox" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *inboxPred = [NSPredicate predicateWithFormat:@"archived == 0 AND cardId != 0"];
[fetchRequest setPredicate:inboxPred];
NSSortDescriptor *sortDescriptor2 = [[[NSSortDescriptor alloc] initWithKey:sortString ascending:sortAsc selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
NSArray *sortDescriptors = [[[NSArray alloc] initWithObjects:sortDescriptor2, nil] autorelease];
[fetchRequest setSortDescriptors:sortDescriptors];
NSError *error = nil;
NSArray *cardTitles = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
[self.ttsInboxCards appendString:[[cardTitles valueForKey:@"cardTitle"] description]];
文字转语音
if (isSpeaking) {
[vocalizer cancel];
isSpeaking = NO;
}
else {
isSpeaking = YES;
vocalizer = [[SKVocalizer alloc] initWithLanguage:@"de_DE" delegate:self];
[vocalizer speakString:tmp];
}
最佳答案
我写道:
cardTitles is an array. So, invoking -valueForKey: on it yields an array built by invoking -valueForKey:@"cardTitle" on each element. Then you're asking that array for its description. It's very likely to do formatting of its contents.
brush51 写道:
componentsJoinedByString: worked and solved the issue with encoding. now i have to split them correctly so that i can use it cleanly for text to speech. can you make an answer with that so i can mark this question as answered and others can find it faster
关于objective-c - NSMutableString 和(字符)编码问题 : getting\U00fc in case of ü,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10086744/