Skip to content

Instantly share code, notes, and snippets.

View efremidze's full-sized avatar
👨‍💻

Lasha Efremidze efremidze

👨‍💻
View GitHub Profile
@efremidze
efremidze / retainCycle.swift
Created April 27, 2017 18:08
Unowned self is unnecessary for lazy variables that aren't closures
import UIKit
class View: UIView {
lazy var label: UILabel = {
let label = UILabel()
self.addSubview(label)
return label
}()
}
extension UIView {
@discardableResult
func constrain(attribute: NSLayoutAttribute, constant: CGFloat = 0) -> NSLayoutConstraint {
return constrain {[
NSLayoutConstraint(item: $0, attribute: attribute, relatedBy: .equal, toItem: $0.superview!, attribute: attribute, multiplier: 1, constant: constant)
]}.first!
}
@discardableResult
func constrainToEdges(_ inset: UIEdgeInsets = .zero) -> [NSLayoutConstraint] {
import RealmSwift
protocol ObjectProtocol {}
extension ObjectProtocol where Self: Object {
static func objects() -> Results<Self> {
return try! Realm().objects(self)
}
static func object(_ primaryKey: Any) -> Self? {
return try! Realm().object(ofType: self, forPrimaryKey: primaryKey)
extension MKMapRect {
init(minX: Double, minY: Double, maxX: Double, maxY: Double) {
self.init(x: minX, y: minY, width: abs(maxX - minX), height: abs(maxY - minY))
}
init(x: Double, y: Double, width: Double, height: Double) {
self.init(origin: MKMapPoint(x: x, y: y), size: MKMapSize(width: width, height: height))
}
var minX: Double { return MKMapRectGetMinX(self) }
var minY: Double { return MKMapRectGetMinY(self) }
var midX: Double { return MKMapRectGetMidX(self) }
extension SomeView: UIKeyInput {
var hasText: Bool { return true }
func insertText(_ text: String) {}
func deleteBackward() {}
override var canBecomeFirstResponder: Bool { return true }
}
private struct Stack<U> {
typealias T = (stack: [[U]], current: [U])
private var value: T
var values: [[U]] {
return value.stack + [value.current]
}
init() {
self.value = (stack: [], current: [])
}
mutating func add(toStack element: U) {
protocol SearchResultProtocol: Hashable, Equatable {
var name: String { get }
}
extension SearchResultProtocol {
var hashValue: Int {
return name.hashValue
}
extension UIFont {
func bold() -> UIFont? {
return fontDescriptor.withSymbolicTraits(.traitBold).map { UIFont(descriptor: $0, size: 0) }
}
}
import Foundation
private class Associated<Type>: NSObject {
let value: Type
init(_ value: Type) {
self.value = value
}
}
protocol Associable {}
protocol Constrainable {}
extension Constrainable where Self: UIView {
@discardableResult
func constrain(constraints: (Self) -> [NSLayoutConstraint]) -> [NSLayoutConstraint] {
let constraints = constraints(self)
self.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(constraints)
return constraints