swiftcocoatoll-free-bridging

Why can't NSSpeechsynthesier's class method availableVoices be bridged to a Swift array of strings?


In Apple's Working with Cocoa Frameworks it reads as if the Foundation and Swift Foundation frameworks work together through bridging. However, I noticed that while attempting to use NSSpeechSynthesizer's class method availableVoices() it allows me to received an returned array of NSStrings but not Strings.

This compiles and runs just fine:

let voices = NSSpeechSynthesizer.availableVoices as [NSString]
        print(voices)

However this won't compile:

let voicesTwo = NSSpeechSynthesizer.availableVoices as [String]

Why wouldn't this work if the voiceName documentation shows that VoiceName is a string property?

I see the term 'rawValue' in the VoiceName documentation so is the reasoning having anything to do with this being some sort of an enum?


Solution

  • It looks like NSSpeechSynthesizer.VoiceName is an Enum with a rawValue of String. That is not the same thing as being a string.

    Try using

    NSSpeechSynthesizer.availableVoices.map { $0.rawValue }