'|'不是后缀一元运算符

'|' is not a postfix unary operator

我正在尝试为 AVAudioSession 类别添加选项:

let session = AVAudioSession.sharedInstance()
    let options = .MixWithOthers.toRaw()|.DefaultToSpeaker.toRaw()
    if session.setCategory(AVAudioSessionCategoryPlayAndRecord,
      withOptions: options,
      error: &error){
...
}

我也试过了

let options = .MixWithOthers|.DefaultToSpeaker

但它给了我同样的错误。

如何组合这些选项?

选项的类型为 AVAudioSessionCategoryOptions, 但在

let options = .MixWithOthers | .DefaultToSpeaker

编译器无法从上下文推断类型。你可以写

let options = AVAudioSessionCategoryOptions.MixWithOthers | AVAudioSessionCategoryOptions.DefaultToSpeaker

let options : AVAudioSessionCategoryOptions = .MixWithOthers | .DefaultToSpeaker

session.setCategory(AVAudioSessionCategoryPlayAndRecord,
            withOptions: .MixWithOthers | .DefaultToSpeaker , error: &error)

您实际上需要在 | 周围使用空格。

let options = .MixWithOthers | .DefaultToSpeaker

此外,使用 .rawValue 而不是 .rawValue()(最新的 xcode)