ios - Multiple intervals in single switch-case using tuple Swift 3 -
i new programming , trying switch multiple conditions. have label takes temperature in celsius or fahrenheit in dependence of 1 set, need take value , attribute settings based on temperature interval. how can manage in 1 switch (if possible), know how using 4 switches, it's long code... tried smth :
let todaystringcomponent = double(templabel.text!) let fahrstringcomponent = double(templabel.text!) let yesterdaystringcomponent = double(yesterdaylabel.text!) switch (celsunitbuttonbool, todaystringcomponent!) { case (true?, -100 ... -40) : // gradientimageoutlet.image = uiimage(named: "thermo_01.png")! if uidevice.current.userinterfaceidiom == .pad { temperatureviewcenteraxiscnstr.constant = 315 gradientheight.constant = 0 animatechanges() } else if uidevice.current.userinterfaceidiom == .phone { temperatureviewcenteraxiscnstr.constant = 315 gradientheight.constant = 470 animatechanges() } case (true?, -39 ... -26): // gradientimageoutlet.image = uiimage(named: "thermo_02.png")! if uidevice.current.userinterfaceidiom == .pad { temperatureviewcenteraxiscnstr.constant = 275 gradientheight.constant = 75 animatechanges() } else if uidevice.current.userinterfaceidiom == .phone { temperatureviewcenteraxiscnstr.constant = 315 gradientheight.constant = 470 animatechanges() } default: print("wth") }
is possible manage in 1 switch can following:
1)check if it's fahr or cels,
2)if it's cels check intervals celsius , stuff
3)if it's fahr check intervals fahrenheit , stuff
4)and yesterdaylabel (with same intervals todaylabel) other stuff.
hope understand mean. thank you.
encapsulating logic enum simplify client code following:
let celsunitbuttonbool = true let todaystringcomponent = -50 let sample = temperature(cellbuttonispressed: celsunitbuttonbool, value: todaystringcomponent) switch (sample, sample.value) { case (.fahrenheit, -100 ... -40): break case (.celsius, -39 ... -26): break default: break }
the implementation of temperature
rather straightforward.
enum temperature { case fahrenheit(value: int) case celsius(value: int) } extension temperature { init(cellbuttonispressed: bool, value: int) { if cellbuttonispressed { self = .celsius(value: value) } else { self = .fahrenheit(value: value) } } var value: int { switch self { case .fahrenheit(value: let value), .celsius(value: let value): return value } } }
Comments
Post a Comment