I need to check if the hour selected by the user is outside of working hours.
I am trying to achieve this result with a for in loop and switch statement, but I am not sure how to implement this.
If var components matches a value in the timeRestricted array, it should break from the switch statement. Thanks for your help.
class FourthViewController: UIViewController {
@IBOutlet weak var myDatePicker: UIDatePicker!
var currentTime = NSDate()
let timeRestricted = [00,01,02,03,04,05,06,19,20,21,22,23]
var dateComponents = NSDateComponents()
var dateFormatter:NSDateFormatter = NSDateFormatter()
@IBOutlet weak var someLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm"
// load current time as minimum date
self.myDatePicker.minimumDate = NSDate(timeInterval: 1, sinceDate: currentTime)
myDatePicker.addTarget(self, action: #selector(self.datePickerAction(_:)), forControlEvents: UIControlEvents.ValueChanged)
}
func datePickerAction(sender: UIDatePicker) {
var timeSelected = sender.date
var unitFlags: NSCalendarUnit = [.Hour]
var components = NSCalendar.currentCalendar().components(unitFlags, fromDate: timeSelected)
for components in timeRestricted {
switch components {
case timeRestricted[0...12]: print("update label")
default: print(" record input data and segue to next screen")
}
}
}
}