枚举 – Swift – 在switch语句中使用enum
发布时间:2020-12-14 02:26:47 所属栏目:百科 来源:网络整理
导读:我收到此错误: 'NSNumber' is not a subtype of Cat 这是代码: enum Cat:Int { case Siamese = 0 case Tabby case Fluffy}let cat = indexPath.row as Cat switch cat { case .Siamese: //do something break; case .Tabby: //do something else break; ca
我收到此错误:
'NSNumber' is not a subtype of Cat 这是代码: enum Cat:Int { case Siamese = 0 case Tabby case Fluffy } let cat = indexPath.row as Cat switch cat { case .Siamese: //do something break; case .Tabby: //do something else break; case .Fluffy: break; } 我该如何解决这个错误?
使用Cat.fromRaw(indexPath.row)获取枚举.
因为fromRaw()的返回值是可选的,所以使用它如下: if let cat = Cat.fromRaw (indexPath.row) { switch cat { // ... } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |