Skip to content

Instantly share code, notes, and snippets.

View Otbivnoe's full-sized avatar

Nikita Ermolenko Otbivnoe

View GitHub Profile
@alongotv
alongotv / OnDidAppearSwiftUI
Last active February 10, 2023 17:21
Adds viewDidAppear callback to SwiftUI
struct ViewControllerLifecycleHandler: UIViewControllerRepresentable {
func makeCoordinator() -> ViewControllerLifecycleHandler.Coordinator {
Coordinator(onDidAppear: onDidAppear)
}
let onDidAppear: () -> Void
func makeUIViewController(context: UIViewControllerRepresentableContext<ViewControllerLifecycleHandler>) -> UIViewController {
context.coordinator
}
@dsabanin
dsabanin / enable-xcode-debug-menu.sh
Last active November 7, 2022 16:17
Enable internal Xcode debug menu in Xcode 11
defaults write com.apple.dt.Xcode ShowDVTDebugMenu -bool YES
sudo mkdir -p /Applications/Xcode.app/Contents/Developer/AppleInternal/Library/Xcode
sudo touch /Applications/Xcode.app/Contents/Developer/AppleInternal/Library/Xcode/AppleInternal.plist
# Don't forget to restart Xcode
@tclementdev
tclementdev / libdispatch-efficiency-tips.md
Last active July 12, 2024 03:33
Making efficient use of the libdispatch (GCD)

libdispatch efficiency tips

The libdispatch is one of the most misused API due to the way it was presented to us when it was introduced and for many years after that, and due to the confusing documentation and API. This page is a compilation of important things to know if you're going to use this library. Many references are available at the end of this document pointing to comments from Apple's very own libdispatch maintainer (Pierre Habouzit).

My take-aways are:

  • You should create very few, long-lived, well-defined queues. These queues should be seen as execution contexts in your program (gui, background work, ...) that benefit from executing in parallel. An important thing to note is that if these queues are all active at once, you will get as many threads running. In most apps, you probably do not need to create more than 3 or 4 queues.

  • Go serial first, and as you find performance bottle necks, measure why, and if concurrency helps, apply with care, always validating under system pressure. Reuse

@lsavino
lsavino / compilation-optimization.md
Last active July 27, 2022 17:44
Compiler Optimizations, Compiling Optimally, and Whole Modules

DEPRECATED for Xcode 10 🎈

(check out What's New in Swift at 11:40, slide 42)

Whole Module Compilation Optimizations: Why these terms are sometimes misleading

When you look up how to compile swift faster for debug builds, people very earnestly give advice that seems contradictory: you should "try using the whole module optimization flag," and also "never use whole module optimization for debugging". [^1]

This is confusing because some of us are using these two general words:

compilation: "turning text into an executable program"

@artemnovichkov
artemnovichkov / Optional+Operators.swift
Last active July 30, 2018 15:16
Addition and subtraction operators for Swift Optionals
func +=<T>(lhs: inout T?, rhs: T) where T: SignedNumeric {
switch lhs {
case .none:
break
case .some(let value):
lhs = .some(value + rhs)
}
}
func -=<T>(lhs: inout T?, rhs: T) where T: SignedNumeric {
@artemnovichkov
artemnovichkov / ServicesComposition.swift
Last active May 4, 2018 07:17
Using protocol composition and generics for dependency injection in Interactors (VIPER, SOA, another cool keywords...)
//Inspired by: http://merowing.info/2017/04/using-protocol-compositon-for-dependency-injection/
//Protocols for objects owning services. Interactors in VIPER, for example.
protocol HasLogService {
var logService: LogService { get }
}
protocol HasLoginService {
var loginService: LoginService { get }
}
@sooop
sooop / StreamReader.swift
Last active June 23, 2024 22:49
Read a large text file line by line - Swift 3
import Foundation
class StreamReader {
let encoding: String.Encoding
let chunkSize: Int
let fileHandle: FileHandle
var buffer: Data
let delimPattern : Data
var isAtEOF: Bool = false
@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
@dangthaison91
dangthaison91 / then.swift
Last active December 30, 2018 09:36
RxSwift - Then
@jamiepinkham
public extension ObservableType {
func then(closure: () -> Observable<E>?) -> Observable<E> {
return then(closure() ?? .empty())
}
func then(@autoclosure(escaping) closure: () -> Observable<E>) -> Observable<E> {
let next = Observable.deferred {
import UIKit
// Swift rewrite challenge
// Starting point: https://gist.github.com/jkereako/200342b66b5416fd715a#file-scale-and-crop-image-swift
func scaleAndCropImage(
image: UIImage,
toSize size: CGSize,
fitImage: Bool = true
) -> UIImage {