首页 > 解决方案 > 为什么 NSSpeechsynthesier 的类方法 availableVoices 不能桥接到 Swift 字符串数组?

问题描述

在 Apple 的Working with Cocoa Frameworks中,它读起来好像 Foundation 和 Swift Foundation 框架通过桥接一起工作。但是,我注意到在尝试使用 NSSpeechSynthesizer 的类方法 availableVoices() 时,它允许我接收返回的 NSStrings 数组,但不能接收 Strings。

这编译并运行得很好:

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

但是,这不会编译:

let voicesTwo = NSSpeechSynthesizer.availableVoices as [String]

如果voiceName 文档显示 VoiceName 是一个字符串属性,为什么这不起作用?

我在 VoiceName 文档中看到了“rawValue”一词,那么推理是否与某种枚举有关?

标签: swiftcocoatoll-free-bridging

解决方案


它看起来像是NSSpeechSynthesizer.VoiceName一个Enum与一个rawValueString。这与作为字符串不同。

尝试使用

NSSpeechSynthesizer.availableVoices.map { $0.rawValue }

推荐阅读