Skip to content

Instantly share code, notes, and snippets.

Avatar

Alexander Grebenyuk kean

View GitHub Profile
View cURLDescription.swift
extension URLRequest {
public func cURLDescription() -> String {
guard let url = url, let method = httpMethod else {
return "$ curl command generation failed"
}
var components = ["curl -v"]
components.append("-X \(method)")
for header in allHTTPHeaderFields ?? [:] {
let escapedValue = header.value.replacingOccurrences(of: "\"", with: "\\\"")
components.append("-H \"\(header.key): \(escapedValue)\"")
@kean
kean / EnvironmentPicker.swift
Created Nov 21, 2021
Code from "Designing an API Client in Swift"
View EnvironmentPicker.swift
enum APIEnvironment: String, CaseIterable {
case dev
case test
case stage
case prod
}
struct ContentView: View {
@State private var selection: APIEnvironment? = env
@kean
kean / Migration.swift
Created Jun 20, 2016
Core Data Progressive Migration
View Migration.swift
// The MIT License (MIT)
//
// Copyright (c) 2016 Alexander Grebenyuk (github.com/kean).
import Foundation
import CoreData
enum MigrationError: ErrorProtocol {
case IncompatibleModels
}
@kean
kean / AutoRetry.swift
Last active Feb 24, 2022
Smart Auto Retry using RxSwift
View AutoRetry.swift
// The MIT License (MIT)
//
// Copyright (c) 2017 Alexander Grebenyuk (github.com/kean).
import Foundation
import RxSwift
import RxCocoa
extension ObservableType {
@kean
kean / CtCI-6h-problem-4.9.markdown
Last active Feb 4, 2022
CtCI 6h Edition, Problem 4.9: BST Sequences.
View CtCI-6h-problem-4.9.markdown

Problem 4.9. BST Sequences: A binary search tree was created by traversing through an array from left to right and inserting each element. Given a binary search tree with distinct elements, print all possible arrays that could have led to this tree.

Solution.

Let's start with an example.

    4
   / \
 2 5 
View parser-validator.swift
import UIKit
// Almost pseudocode, but should be possible to write in proper Swift
// MARK: High-Level APIs
// Decodes the document. By default, ignores errors and warnings.
func decode(data: Data, isStrict: Bool = false) -> throws OpenAPI.Document {
let parser = JSONDecoder().decoder(DocumentParser.self, from: data)
// Or maybe be even more granular: "strict", "ignoreWarnings", "ignoreAll"?
@kean
kean / Client.swift
Last active Nov 21, 2021
API Client (Archived)
View Client.swift
// The MIT License (MIT)
//
// Copyright (c) 2017 Alexander Grebenyuk (github.com/kean).
import Foundation
import Alamofire
import RxSwift
import RxCocoa
// This post is **archived**. For a modern version that uses Async/Await and Actors, see the new article
@kean
kean / Pulse-Pro-1.4.md
Created Nov 9, 2021
Pulse Pro 1.4 Release Notes
View Pulse-Pro-1.4.md

Pulse 1.4.0

Nov 8, 2021

Navigation

  • You can now switch to a Network view using the main Toolbar. It has several advantages. For example, when you switch, it keeps your selection, filters, and other perferences. You can now also close the sidebar if you don't need to switch between devices.
  • Sidebar now displayes your connected devices making it easier to switch between them. Devices and their associated logs are stored persistently. With a context menu, you can open the device in a separate window, show the store in Finder, or remove the device.
  • When you open a log file (.pulse extension), it nows opens in a clean window with no sidebar and with a filename in the navigation title.
View PinEdges.swift
public protocol LayoutItem { // `UIView`, `UILayoutGuide`
var superview: UIView? { get }
}
extension UIView: LayoutItem {}
extension UILayoutGuide: LayoutItem {
public var superview: UIView? { owningView }
}
public struct Alignment {
View FocusList.swift
import SwiftUI
@main
struct FocusTestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.commands {
MessageCommands()