ios – AVAudioPlayer不再使用Swift 2.0Xcode 7 beta

ios – AVAudioPlayer不再使用Swift 2.0Xcode 7 beta,第1张

概述对于我的iPhone应用程序中的var testAudio声明,我在这里收到错误 “调用可以抛出,但错误不能从属性初始化程序中抛出” import UIKitimport AVFoundationclass ViewController: UIViewController { var testAudio = AVAudioPlayer(contentsOfURL: NSURL (fil 对于我的iPhone应用程序中的var testAudio声明,我在这里收到错误

“调用可以抛出,但错误不能从属性初始化程序中抛出”

import UIKitimport AVFoundationclass VIEwController: UIVIEwController {    var testAudio = AVAudioPlayer(contentsOfURL: NSURL (fileURLWithPath: NSBundle.mainBundle().pathForResource("testAudio",ofType: "wav")!),fileTypeHint:nil)

当我转到Xcode 7测试版时,就发生了这种情况.

如何在Swift 2.0中使用此音频剪辑?

解决方法 Swift 2有一个全新的错误处理系统,您可以在这里阅读更多相关信息: Swift 2 Error Handling.

在您的情况下,AVAudioPlayer构造函数可能会抛出错误. Swift不会让你使用在属性初始化器中抛出错误的方法,因为没有办法在那里处理它们.相反,不要在视图控制器的init之前初始化属性.

var testAudio:AVAudioPlayer;init() {    do {        try testAudio = AVAudioPlayer(contentsOfURL: NSURL (fileURLWithPath: NSBundle.mainBundle().pathForResource("testAudio",fileTypeHint:nil)    } catch {        //Handle the error    }}

这使您有机会处理创建音频播放器时可能出现的任何错误,并会阻止Xcode向您发出警告.

总结

以上是内存溢出为你收集整理的ios – AVAudioPlayer不再使用Swift 2.0/Xcode 7 beta全部内容,希望文章能够帮你解决ios – AVAudioPlayer不再使用Swift 2.0/Xcode 7 beta所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1098812.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存