AVPlayerItem Swift 2零检查

AVPlayerItem Swift 2零检查,swift,swift2,avfoundation,xcode7,avplayeritem,Swift,Swift2,Avfoundation,Xcode7,Avplayeritem,该代码在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.ap

该代码在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”和“Nilliteral”


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

您可以声明一个可选的:

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

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

谢谢你解决了我的问题,这样我完全忘记了