Swift:Call can throw, but it is not marked with 'try' and the error is not handled

Swift:Call can throw, but it is not marked with 'try' and the error is not handled,第1张

概述对习惯了Object-C语法的人来说,Swift有些语法格式真的差别挺大的 对于API的错误处理,他们也是不一样的机制,下面以AVAudioPlayer的一个初始化方法为例 - initWithContentsOfURL:error: 首先看下官方文档的定义: 在Object-C下可以直接传递NSError的参数来处理错误 但在Swift 2.2开始,它的错误处理必须放在do语句的catch子句中

对习惯了Object-C语法的人来说,Swift有些语法格式真的差别挺大的

对于API的错误处理,他们也是不一样的机制,下面以AVAudioPlayer的一个初始化方法为例

- initWithContentsOfURL:error:

首先看下官方文档的定义:



在Object-C下可以直接传递NSError的参数来处理错误


但在Swift 2.2开始,它的错误处理必须放在do语句的catch子句中,本身方法必须放在try语句中



如果还是按照Object-C的思维调用


则会报如下的错误: "Call can throw,but it is not marked with 'try' and the error is not handled"

总结

以上是内存溢出为你收集整理的Swift:Call can throw, but it is not marked with 'try' and the error is not handled全部内容,希望文章能够帮你解决Swift:Call can throw, but it is not marked with 'try' and the error is not handled所遇到的程序开发问题。

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

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

原文地址: https://outofmemory.cn/web/1073573.html

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

发表评论

登录后才能评论

评论列表(0条)

保存