Skip to content

Instantly share code, notes, and snippets.

View vialyx's full-sized avatar
🎯
Focusing

Maxim Vialyx vialyx

🎯
Focusing
View GitHub Profile
// TODO: - Create a new error
var error = EmailValidationError.empty
// TODO: - OR type-inferred
var signupError: EmailValidationError = .invalid
error = .noDomain
switch error {
case .empty:
print("Please provide email")
case .noDomain:
print("Add domain")
case .invalid:
print("Entered email looks like invaid")
}
if signupError == .invalid {
enum EmailValidationError: CaseIterable {
// TODO: - Cases
case empty
// TODO: - Cases can be declared on a single line
case noDomain, invalid
}
print("All cases: \(EmailValidationError.allCases)")
// TODO: - Just print all enum cases
enum EmailValidationError {
// TODO: - Cases
case empty
// TODO: - Cases can be declared on a single line
case noDomain(String)
case invalid(String)
}
// TODO: - Create a new error
var error = EmailValidationError.empty
enum Weekday: Int {
case sun
case mon
case tue
case wed
case thur
case fri
case sat
var value: String {
enum EmailValidationError: Error {
// TODO: - Cases
case empty
// TODO: - Cases can be declared on a single line
case noDomain(String)
case invalid(String)
}
extension EmailValidationError: LocalizedError {
var date = Date(timeIntervalSince1970: 50 * 365 * 24 * 60 * 60)
print("date: \(date)")
// console: date: 2019-12-20 00:00:00 +0000
let now = Date()
var future = now
print("equal: \(now == future)")
// equal: true
future += 1 * 60 * 60
print("not equal: \(now == future)")
// not equal: false
future -= 2 * 60 * 60
print("now > future?: \(now > future)")
extension Date {
func compareTime(with date: Date) -> ComparisonResult {
var components = DateComponents()
components.hour = calendar.component(.hour, from: self)
components.minute = calendar.component(.minute, from: self)
var withComponents = DateComponents()
withComponents.hour = calendar.component(.hour, from: date)
withComponents.minute = calendar.component(.minute, from: date)
let startOfDay = Calendar.current.startOfDay(for: Date())
let endOfDay = startOfDay + 8 * 60 * 60
let dateInterval = DateInterval(start: startOfDay, end: endOfDay)
let startLunch = startOfDay + 4 * 60 * 60
let lunchInterval = DateInterval(start: startLunch, duration: 60 * 60)
print("intersection: \(String(describing: dateInterval.intersection(with: lunchInterval)))")
// intersection: Optional(2018-09-17 01:00:00 +0000 to 2018-09-17 02:00:00 +0000)
print("intersects: \(dateInterval.intersects(lunchInterval))")