1

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

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];
    }
}
4

2 回答 2

4

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

于 2011-07-11T19:26:50.307 回答
2

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

if([voiceGender isEqualToString:NSVoiceGenderMale]){
    // etc.
于 2011-07-11T19:26:19.173 回答