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 / 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
@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+Magic.swift
Created December 15, 2016 05:25
UIView with magic
import UIKit
extension UIView {
func вжух() {
setNeedsLayout()
layoutIfNeeded()
}
}
@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 / 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 / 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 / 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 / NSViewController+Loading.swift
Created January 26, 2017 07:26
NSViewController category for simple initialization from storyboard/nib.
import AppKit
extension NSViewController {
static func load<T>() -> T where T: NSViewController {
return T(nibName: T.className(), bundle: nil)!
}
}
//Usage:
//let viewController: LoginViewController = .load()
@artemnovichkov
artemnovichkov / .swiftlint.yml
Created January 30, 2017 02:09
Configuration file for SwiftLint
disabled_rules:
- trailing_whitespace
excluded:
- Carthage/
force_try: warning
force_cast: warning
line_length: 180
colon:
apply_to_dictionaries: false
@artemnovichkov
artemnovichkov / KeyboardNotificationObserver.swift
Last active May 10, 2018 05:19
Simple protocol for sugar keyboard notification observing
protocol KeyboardNotificationObserver {
typealias Selectors = (show: Selector, hide: Selector)
func addKeyboardWillChangeObservers(with selectors: Selectors)
func removeKeyboardWillChangeObservers()
func addKeyboardDidChangeObservers(with selectors: Selectors)
func removeKeyboardDidChangeObservers()
}