【问题标题】:AVPlayerItem Swift 2 Nil CheckingAVPlayerItem Swift 2 Nil 检查
【发布时间】:2015-10-08 22:00:50
【问题描述】:

代码在 Swift 1.2 中运行良好,但是当我更改 Xcode 7 (Swift 2) 时,出现错误,该错误写在下面的 if 语句中。

let objFirstSound = AVPlayerItem(URL:NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(self.objVowels?.main_sound, ofType: "mp3") ?? ""))

if objFirstSound != nil {
   arrTemp.append(objFirstSound)
}

二元运算符“!=”不能应用于类型的操作数 'AVPlayerItem' 和 'NilLiteralConvertible'

那么,我如何检查AVPlayerItem实际上是Nil

【问题讨论】:

    标签: swift swift2 avfoundation xcode7 avplayeritem


    【解决方案1】:

    你可以声明一个 Optional:

    let objFirstSound : AVPlayerItem? = AVPlayerItem(URL:NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(self.objVowels?.main_sound, ofType: "mp3") ?? ""))
    

    然后做:

    if let objFirstSound = objFirstSound
    {
        arrTemp.append(objFirstSound)
    }
    

    【讨论】:

    • 谢谢你解决了我的问题,我完全忘记了这种方式
    猜你喜欢
    • 2016-04-07
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    相关资源
    最近更新 更多