Skip to content

Instantly share code, notes, and snippets.

View vialyx's full-sized avatar
🎯
Focusing

Maxim Vialyx vialyx

🎯
Focusing
View GitHub Profile
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. ???
struct Buffer<Element> {
enum BufferError: Error {
case outOfBounds
}
private var items: [Element] = []
mutating func add(_ item: Element) {
items.append(item)
extension Buffer {
var isEmpty: Bool {
return items.isEmpty
}
var middle: Element? {
return try? get(index: items.count / 2)
}
func someFunction<T: SomeClass, U: SomeProtocol>(someT: T, someU: U) {
// function body goes here
}
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)
}
protocol APIObjectMapping {
associatedtype Object
static func with(object: Object?) -> Self?
}
protocol ClientObjectMapping {
associatedtype Object
var object: Object? { get }
}
enum TaskType: String, Codable {
case unknown
case activity
}
// MARK: - APIObjectMapping
extension TaskType: APIObjectMapping {
typealias Object = String
static func with(object: String?) -> TaskType? {
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,
infix operator +++: AdditionPrecedence
extension Engine {
static func +++ (left: Engine, right: Engine) -> Engine {
let tank = max(left.tank, right.tank)
let consumption = min(left.consumption, right.consumption)
return Engine(tank: tank,
consumption: consumption,
distance: tank / consumption * 100,
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var contentView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Write that code
contentView.backgroundColor = .yellow