数据模型Model(I)
程序员文章站
2022-04-12 16:52:01
枚举、结构体和协议组成Model 1 //定义一个协议 2 protocol BaseItemProtocal { 3 var title: String { get set } //属性是可读可写的 4 var type: CriteriaType { get set } 5 } 6 7 stru ......
枚举、结构体和协议组成Model
1 //定义一个协议 2 protocol BaseItemProtocal { 3 var title: String { get set } //属性是可读可写的 4 var type: CriteriaType { get set } 5 } 6 7 struct FilterCondition { 8 var title: String 9 var items: [BaseItemProtocal] 10 } 11 12 struct BaseNumberItem: BaseItemProtocal { 13 var title: String 14 var type: CriteriaType 15 var valueNumber: Int 16 } 17 18 struct BaseRangeItem: BaseItemProtocal { 19 var title: String 20 var type: CriteriaType 21 var valueRange: NSRange 22 } 23 24 enum CriteriaType { 25 case DiabetesType //糖尿病类型 26 case Age //年龄 27 case DiagnosisYears //糖龄 28 case HsLevel //会员类型 29 case ReportStatus //血糖报告 30 31 func title() -> String { 32 switch self { 33 case .DiabetesType: return "糖尿病类型" 34 case .Age: return "年龄" 35 case .DiagnosisYears: return "糖龄(年)" 36 case .HsLevel: return "会员类型" 37 case .ReportStatus: return "血糖报告" 38 } 39 } 40 41 42 func subTitles() -> [String] { 43 switch self { 44 case .DiabetesType: return ["一型", "二型", "妊娠", "其他"] 45 case .Age: return ["0-20", "21-40", "41-60", "61-80", "80以上"] 46 case .DiagnosisYears: return ["0-5", "6-10", "11-15", "16-20", "21-25", "26-30", "30以上"] 47 case .HsLevel: return ["银卡", "金卡", "白金卡", "钻石卡", "非会员"] 48 case .ReportStatus: return ["已生成", "未生成"] 49 } 50 } 51 52 53 func items() -> [BaseItemProtocal] { 54 switch self { 55 case .DiabetesType: 56 return [BaseNumberItem(title: "一型", type: .DiabetesType, valueNumber: 1), 57 BaseNumberItem(title: "二型", type: .DiabetesType, valueNumber: 2), 58 BaseNumberItem(title: "妊娠", type: .DiabetesType, valueNumber: 3), 59 BaseNumberItem(title: "其他", type: .DiabetesType, valueNumber: 4)] 60 case .Age: 61 return [BaseRangeItem(title: "0-20", type: .Age, valueRange: NSMakeRange(0, 20)), 62 BaseRangeItem(title: "21-40", type: .Age, valueRange: NSMakeRange(21, 40)), 63 BaseRangeItem(title: "41-60", type: .Age, valueRange: NSMakeRange(41, 60)), 64 BaseRangeItem(title: "61-80", type: .Age, valueRange: NSMakeRange(61, 80)), 65 BaseRangeItem(title: "80以上", type: .Age, valueRange: NSMakeRange(81, Int.max))] 66 case .DiagnosisYears: 67 return [BaseRangeItem(title: "0-5", type: .DiagnosisYears, valueRange: NSMakeRange(0, 5)), 68 BaseRangeItem(title: "6-10", type: .DiagnosisYears, valueRange: NSMakeRange(6, 10)), 69 BaseRangeItem(title: "11-15", type: .DiagnosisYears, valueRange: NSMakeRange(11, 15)), 70 BaseRangeItem(title: "16-20", type: .DiagnosisYears, valueRange: NSMakeRange(16, 20)), 71 BaseRangeItem(title: "21-25", type: .DiagnosisYears, valueRange: NSMakeRange(21, 25)), 72 BaseRangeItem(title: "26-30", type: .DiagnosisYears, valueRange: NSMakeRange(26, 30)), 73 BaseRangeItem(title: "30以上", type: .DiagnosisYears, valueRange: NSMakeRange(31, Int.max))] 74 case .HsLevel: 75 return [BaseNumberItem(title: "银卡", type: .HsLevel, valueNumber: 1), 76 BaseNumberItem(title: "金卡", type: .HsLevel, valueNumber: 2), 77 BaseNumberItem(title: "白金卡", type: .HsLevel, valueNumber: 3), 78 BaseNumberItem(title: "钻石卡", type: .HsLevel, valueNumber: 4), 79 BaseNumberItem(title: "非会员", type: .HsLevel, valueNumber: 0)] 80 case .ReportStatus: 81 return [BaseNumberItem(title: "已生成", type: .ReportStatus, valueNumber: 1), 82 BaseNumberItem(title: "未生成", type: .ReportStatus, valueNumber: 0)] 83 } 84 } 85 } 86 87 88 89 90 91 92 93 94 //使用 95 let title = CriteriaType.DiabetesType.title() 96 print("---------->枚举:\(title)") 97 98 let items = CriteriaType.DiabetesType.items() 99 100 print("----------------->枚举数组:\(items)")
打印结果:
---------->枚举:糖尿病类型
----------------->枚举数组:[SwiftTest.BaseNumberItem(title: "一型", type: SwiftTest.CriteriaType.DiabetesType, valueNumber: 1), SwiftTest.BaseNumberItem(title: "二型", type: SwiftTest.CriteriaType.DiabetesType, valueNumber: 2), SwiftTest.BaseNumberItem(title: "妊娠", type: SwiftTest.CriteriaType.DiabetesType, valueNumber: 3), SwiftTest.BaseNumberItem(title: "其他", type: SwiftTest.CriteriaType.DiabetesType, valueNumber: 4)]
上一篇: Intel在WiMAX上投资逾10亿美元
推荐阅读