ios - XCode 7 Swift - 调用可以抛出,它没有标记为 'try' 并且错误没有被处理

标签 ios xcode avfoundation

当我点击一个按钮时,我试图在我的 iOS 应用程序上播放音乐,我已经知道该怎么做,但是这次当我插入一行时; audioPlayer = AVAudioPlayer(contentsOfURL: audioPlayerURL, error: nil)它会给我一个选项,可以将 error 更改为 fileTypeHint 我这样做了,但后来我出现以下错误; `Call can throw, it it is not markedwith 'try' and the error is not handled'.

我不太明白,因为在 Xcode 6 中,我在那行代码中使用了 error 这个词,我对此没有任何问题,并且可以播放音乐。自从我升级到 Xcode 7 Beta 3 以来,一定有不同或相似的方式来编写该行,但我到处搜索以查看发生了什么变化。

有人知道吗?

最佳答案

错误处理在 Swift 2.0 中发生了变化。使用以下 try-catch 代码解决错误:

    do {
        audioPlayer = try AVAudioPlayer(contentsOfURL: audioFileUrl,
                                        fileTypeHint: AVFileTypeMPEGLayer3)
    } catch let error as NSError {
        print("AV Sound Error: \(error.localizedDescription)")
    }

关于ios - XCode 7 Swift - 调用可以抛出,它没有标记为 'try' 并且错误没有被处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31420209/

相关文章:

ios - 从 NSString ios 获取日期

ios - UITableViewCell drawRect 底部边框在 insertRow 上消失

ios - 子类化 UICollectionViewLayoutAttributes

iphone - 如何在 UIView 中获取父级导航 Controller

ios - AVAssetResourceLoader 支持的 AVURLAsset 播放仅通过 AirPlay 失败

ios - UIImageView 显示扭曲的图像

ios - "See Through"以编程方式产生效果

ios - 如何等待 UI 元素而不导致测试用例失败

swift - 如何在 Swift 的 UI 中添加视频

ios - 如何将 CIImage 保存到 Photos 并保持其 P3 色彩空间完好无损?