开关语句中的字符串:’String’不符合协议’IntervalType’

前端之家收集整理的这篇文章主要介绍了开关语句中的字符串:’String’不符合协议’IntervalType’前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在Swift的switch语句中使用字符串时遇到问题。

我有一个名为opts的字典被声明为< String,AnyObject>

我有这个代码

@H_301_5@switch opts["type"] { case "abc": println("Type is abc") case "def": println("Type is def") default: println("Type is something else") }

在“abc”和case“def”的情况下,我得到以下错误

@H_301_5@Type 'String' does not conform to protocol 'IntervalType'

有人可以向我解释我做错了什么吗?

在switch语句中使用可选项时会显示错误
简单地展开变量,一切都应该起作用。 @H_301_5@switch opts["type"]! { case "abc": println("Type is abc") case "def": println("Type is def") default: println("Type is something else") }

编辑:
如果您不想强制展开可选项,可以使用防护。参考:Control Flow: Early Exit

猜你在找的Swift相关文章