如何使用枚举来简化和改进它,快速学习,枚举 [英] how to simplify and make it better using enum, swift learning, enum
本文介绍了如何使用枚举来简化和改进它,快速学习,枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在通过以下方式定义一些范围
I am defining some of ranges by following
let range0_15 = 0 ..< 15
let range15_30 = 15 ..< 30
let range30_45 = 30 ..< 45
let range45_60 = 45 ..< 60
我真的很想尝试使用Enum
enum range : Range<Int> {
range0_15 = 0 ..< 15
range15_30 = 15 ..< 30
range30_45 = 30 ..< 45
range45_60 = 45 ..< 60
}
我遇到错误
Type 'CoreDataStach.range' does not comform to protocol RawRepresentable
是否可以像上面那样定义一个枚举,如果是,那么我在这里缺少什么.
Is it possible to define an enum like above and if it is yes, what I am missing right here.
推荐答案
使用结构体尝试类似方法可能会更好.
You might be better off trying something like this using a struct.
struct Ranges{
static let range0_15 = 0 ..< 15
static let range15_30 = 15 ..< 30
static let range30_45 = 30 ..< 45
static let range45_60 = 45 ..< 60
}
if Ranges.range0_15.contains(6){ //Test to true
print("inside range")//Prints "inside range"
}
这篇关于如何使用枚举来简化和改进它,快速学习,枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文