2011-07-11 61 views
1

我正在制作一个应用程序,其中我试图在Mac OS X中获取所有不同的声音,然后按性别对它们进行排序。我创建了三个可变数组来放置每个性别(男性,女性,新奇)的声音,并且我使用枚举来遍历每个性别并将其放入正确的数组中。不幸的是,这不起作用。除了新颖的阵列之外,所有的新颖阵列都是空的,而新奇阵列只有一个声音,Zarvox。有人看到我做错了吗?我发布了以下代码:Mac OS X文本转语音性别

NSArray* voices = [NSSpeechSynthesizer availableVoices]; 
for(NSString* x in voices){ 

    NSDictionary* voiceInfo = [NSSpeechSynthesizer attributesForVoice:x]; 

    NSString* voiceName = [voiceInfo objectForKey:NSVoiceName]; 
    NSString* voiceGender = [voiceInfo objectForKey:NSVoiceGender]; 


    maleVoices = [[NSMutableArray alloc] init]; 
    femaleVoices = [[NSMutableArray alloc] init]; 
    noveltyVoices = [[NSMutableArray alloc] init]; 

    if (voiceGender == NSVoiceGenderMale){ 
     [maleVoices addObject:voiceName]; 

    } else if (voiceGender == NSVoiceGenderFemale) { 
     [femaleVoices addObject:voiceName]; 
    } else { 

     [noveltyVoices addObject:voiceName]; 
    } 
} 
+0

您是否试过使用'isEqual:'方法而不是'=='比较'voiceGender'变量? –

回答

4

分配maleVoicesfemaleVoicesnoveltyVoicesfor循环之外。您只需在循环的每次迭代中创建一个新的空数组。

+0

谢谢!那正是我需要的! – thekmc

2

与字符串的直接相等比较通常是不可靠的。使用-isEqualToString:方法:

if([voiceGender isEqualToString:NSVoiceGenderMale]){ 
    // etc. 
+0

谢谢!虽然这对我的主要问题没有帮助,但它有助于解决后来出现的另一个问题。 – thekmc