Skip to content

Instantly share code, notes, and snippets.

View artemnovichkov's full-sized avatar
👨‍💻
Write code. Blow minds.

Artem Novichkov artemnovichkov

👨‍💻
Write code. Blow minds.
View GitHub Profile
@artemnovichkov
artemnovichkov / ObservableType+Sugar.swift
Last active January 30, 2017 02:12
Syntax sugar for ObservableType
import RxSwift
extension ObservableType {
typealias EmptyHandler = () -> Void
func onNext(_ onNext: @escaping (E) -> Void) -> Observable<E> {
return `do`(onNext: onNext)
}
@artemnovichkov
artemnovichkov / Benchmark.swift
Created January 23, 2017 03:46
Function for check algorithm efficiency
func benchmark(repeatCount: Int = 1, name: String? = nil, closure: () -> Void) {
let d = CACurrentMediaTime()
for _ in 0..<repeatCount {
closure()
}
let d1 = CACurrentMediaTime() - d
print("Benchmark of \(name ?? "closure") took \(d1) seconds")
}
@artemnovichkov
artemnovichkov / ColorSet.swift
Last active January 30, 2017 02:15
Elegant enum for custom colors worked on both iOS and macOS
#if os(iOS)
import UIKit
#elseif os(OSX)
import AppKit
#endif
fileprivate struct ColorComponents {
let first: CGFloat
let second: CGFloat
@artemnovichkov
artemnovichkov / CustomOperators.swift
Last active April 13, 2018 03:31
Bitwise AND assignment operator like in Objective-C
func &=(lhs: inout Bool, rhs: @autoclosure () -> Bool) {
lhs = lhs && rhs()
}
infix operator ||=: AssignmentPrecedence
func ||=(lhs: inout Bool, rhs: @autoclosure () -> Bool) {
lhs = lhs || rhs()
}
@artemnovichkov
artemnovichkov / UIView+Magic.swift
Created December 15, 2016 05:25
UIView with magic
import UIKit
extension UIView {
func вжух() {
setNeedsLayout()
layoutIfNeeded()
}
}
@artemnovichkov
artemnovichkov / UITableView+Dequeue.swift
Created December 9, 2016 08:21
UItableView dequeueing with Swift magic
import UIKit
extension UITableView {
func dequeueReusableCell<T: UITableViewCell>() -> T {
return dequeueReusableCell(withIdentifier: NSStringFromClass(T.self)) as! T
}
}
//using: let cell: ExampleTableViewCell = tableView.dequeueReusableCell()
@artemnovichkov
artemnovichkov / UIView+Presentation.swift
Last active February 11, 2017 07:31
Extension for rounded corners in UIView. Warning: make sure that your UIView already has correct frame.
import UIKit
extension UIView {
func round(with radius: CGFloat, corners: UIRectCorner) {
let roundedPath = UIBezierPath(roundedRect: bounds,
byRoundingCorners: corners,
cornerRadii: CGSize(width: radius, height: radius))
let maskLayer = CAShapeLayer()
maskLayer.path = roundedPath.cgPath