Important extensions while create a new App in swift
protocol Supporter {
var isEmptyValue: Bool { get }
var isNotEmptyValue: Bool { get }
}
protocol DecimalSupporter {
var isNegative: Bool { get }
var isPositive: Bool { get }
}
protocol DoubleSupporter {
var toDouble: Double { get }
}
protocol FloatSupporter {
var toFloat: Float { get }
}
protocol IntSupporter {
var toInt: Int { get }
}
protocol StringSupporter {
var toString: String { get }
}
protocol DateSupporter {
var toDate: Date? { get }
}
protocol DateWithFormateSupporter {
func toDate(_ formate: String)-> Date?
}
extension String: Supporter, DoubleSupporter, FloatSupporter, IntSupporter, DateSupporter, DateWithFormateSupporter {
var toDate: Date? {
let dateFormatter = DateFormatter()
return dateFormatter.date(from: self)
}
func toDate(_ formate: String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = formate
return dateFormatter.date(from: self)
}
var isEmptyValue: Bool { return self.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty }
var isNotEmptyValue: Bool { return !self.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty }
var toFloat: Float { return Float(self) ?? 0 }
var toInt: Int { return Int(self) ?? 0 }
var toDouble: Double { return Double(self) ?? 0 }
}
extension Int: Supporter, DecimalSupporter, DoubleSupporter, FloatSupporter, StringSupporter, DateSupporter {
var toDate: Date? {
Date.init(timeIntervalSince1970: TimeInterval(self))
}
var isEmptyValue: Bool { return self == 0}
var isNotEmptyValue: Bool { return self != 0 }
var isNegative: Bool { return self < 0 }
var isPositive: Bool { return self > 0 }
var toString: String { return String(self) }
var toDouble: Double { return Double(self) }
var toFloat: Float { return Float(self) }
}
extension Double:Supporter, DecimalSupporter, IntSupporter, FloatSupporter, StringSupporter, DateSupporter {
var toDate: Date? {
Date.init(timeIntervalSince1970: TimeInterval(self))
}
var isEmptyValue: Bool { return self == 0}
var isNotEmptyValue: Bool { return self != 0 }
var isNegative: Bool { return self < 0 }
var isPositive: Bool { return self > 0 }
var toString: String { return String(self) }
var toInt: Int { return Int(self) }
var toFloat: Float { return Float(self) }
}
extension Float: Supporter, DecimalSupporter, IntSupporter, DoubleSupporter, StringSupporter, DateSupporter {
var toDate: Date? {
Date.init(timeIntervalSince1970: TimeInterval(self))
}
var isEmptyValue: Bool { return self == 0}
var isNotEmptyValue: Bool { return self != 0 }
var isNegative: Bool { return self < 0 }
var isPositive: Bool { return self > 0 }
var toString: String { return String(self) }
var toInt: Int { return Int(self) }
var toDouble: Double { return Double(self) }
}
extension Date: StringSupporter {
var timestampInMilliseconds: Int { return Int(self.timeIntervalSince1970 * 1000) }
var toString: String { return "\(self)" }
func toString(_ formate: String)-> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = formate
return dateFormatter.string(from: self)
}
func addDays(_ value: Int) -> Date? {
return Calendar.current.date(byAdding: .day, value: value, to: self)
}
func addHours(_ value: Int) -> Date? {
return Calendar.current.date(byAdding: .hour, value: value, to: self)
}
func addYears(_ value: Int) -> Date? {
return Calendar.current.date(byAdding: .year, value: value, to: self)
}
}
Comments
Post a Comment