枚舉、結構體和協議組成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)")View Code
列印結果:
---------->枚舉:糖尿病類型
----------------->枚舉數組:[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)]