Skip to content

Instantly share code, notes, and snippets.

View vialyx's full-sized avatar
🎯
Focusing

Maxim Vialyx vialyx

🎯
Focusing
View GitHub Profile
postfix operator ~~~
struct Engine {
var tank: Float
var consumption: Float
var distance: Float
var consumptionTarget: Float?
static prefix func ~ (engine: Engine) -> Engine {
return Engine(tank: engine.tank,
enum TaskType: String, Codable {
case unknown
case activity
}
// MARK: - APIObjectMapping
extension TaskType: APIObjectMapping {
typealias Object = String
static func with(object: String?) -> TaskType? {
protocol APIObjectMapping {
associatedtype Object
static func with(object: Object?) -> Self?
}
protocol ClientObjectMapping {
associatedtype Object
var object: Object? { get }
}
protocol ReusableViewType: class {
static var defaultReuseIdentifier: String { get }
static var nib: UINib { get }
}
extension ReusableViewType where Self: UIView {
static var defaultReuseIdentifier: String {
return NSStringFromClass(self)
}
func someFunction<T: SomeClass, U: SomeProtocol>(someT: T, someU: U) {
// function body goes here
}
extension Buffer {
var isEmpty: Bool {
return items.isEmpty
}
var middle: Element? {
return try? get(index: items.count / 2)
}
struct Buffer<Element> {
enum BufferError: Error {
case outOfBounds
}
private var items: [Element] = []
mutating func add(_ item: Element) {
items.append(item)
func swapTwo(_ a: inout Int, _ b: inout Int) {
(a, b) = (b, a)
}
var x = 4
var y = 7
swapTwo(&x, &y)
// What about two string, double, etc. ???
let mediaSet: [Media] = [Video(url: "http://[email protected]", thumb: "http://[email protected]"),
Video(url: "http://[email protected]", thumb: "http://[email protected]"),
Media(url: "http://[email protected]")]
for media in mediaSet {
// We don't sure about casting result
if let video = media as? Video {
print("Video: \(video)")
}
class Media {
var url: String
init(url: String) {
self.url = url
}
}
class Video: Media {