简体   繁体   中英

iOS 7 Text to Speech Crash

I am making an ios app in obj-c and I want the emails spoken to me. But the app crashes at this line: [synthesizer speakUtterance:utterance];

This is the method where the email is being spoken:

-(void) speakEmails {
    NSString *currentEmail = [summariesList objectAtIndex:0];
    NSLog(@"Email Being Spoken: %@", currentEmail);

    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:currentEmail];
    utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];

    AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc]init];
    [synthesizer speakUtterance:utterance];
}

This is what the NSString currentEmail is:

 Email Being Spoken: {
    date = Today;
    sender = "tony@starkindustries.com";
    summary = "Blah Blah Blah Blah";
    type = regular;
}

I keep getting the error -[__NSCFDictionary length]: unrecognized selector sent to instance 0x16528b30 . I don't know why this is happening. Any help is appreciated.

You're setting currentEmail to be a dictionary (parsed JSON) rather than a string. Try this instead:

NSString *currentEmail = [[summariesList objectAtIndex:0] objectForKey:@"sender"];

To ensure this doesn't happen in the future, you might do a quick check like so:

-(void) speakEmails {
    NSString *currentEmail = [summariesList objectAtIndex:0];
    NSLog(@"Email Being Spoken: %@", currentEmail);

    if (currentEmail != nil)
    {
        AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:currentEmail];
        utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];

        AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc]init];

        [synthesizer speakUtterance:utterance];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM