Skip to content

Instantly share code, notes, and snippets.

Jasdev Singh jasdev

View GitHub Profile
@jasdev
jasdev / geometryPreference.swift
Last active Apr 24, 2020
objc.io’s geometryPreference modifier and Preference view for propagating up proxied geometries and reading them.
View geometryPreference.swift
import SwiftUI
struct Preference<PreferenceKey: SwiftUI.PreferenceKey, Content: View>: View
where PreferenceKey.Value: Equatable {
private let content: (PreferenceKey.Value) -> Content
@State private var value: PreferenceKey.Value = PreferenceKey.defaultValue
init(
_ key: PreferenceKey.Type,
@jasdev
jasdev / final_cancellation_handling.swift
Last active Apr 20, 2020
Final `ReplaySubject` and `.Subscription` cancellation handling.
View final_cancellation_handling.swift
final class ReplaySubject<Output, Failure: Error>: Subject {
typealias Output = Output
typealias Failure = Failure
private let bufferSize: Int
private var completion: Subscribers.Completion<Failure>?
private var isActive: Bool { /**/ }
@jasdev
jasdev / postfix_type_erasure.swift
Last active Mar 25, 2020
Postfix type erasure in Combine.
View postfix_type_erasure.swift
import Combine
postfix operator ^
postfix func ^ <Publisher: Combine.Publisher>(_ publisher: Publisher)
-> AnyPublisher<Publisher.Output, Publisher.Failure> {
publisher.eraseToAnyPublisher()
}
@jasdev
jasdev / FoundationErrorWrapper.swift
Created Jul 14, 2016
An attempt at a protocol that can imply the present of specific cases
View FoundationErrorWrapper.swift
import Foundation
public protocol FoundationErrorWrapper: ErrorProtocol {
static func wrappingError(_ error: NSError) -> Self
}
enum SomeError: ErrorProtocol, FoundationErrorWrapper {
case a
case wrappingError(NSError)
}
@jasdev
jasdev / CharacterSet+StringLiteralConvertible.swift
Created Jun 30, 2016
CharacterSet StringLiteralConvertible
View CharacterSet+StringLiteralConvertible.swift
extension CharacterSet: StringLiteralConvertible, ExtendedGraphemeClusterLiteralConvertible {
public init(stringLiteral value: String) {
self.init(charactersIn: value)
}
public init(extendedGraphemeClusterLiteral value: ExtendedGraphemeClusterType) {
self.init(charactersIn: value)
}
public init(unicodeScalarLiteral value: UnicodeScalarType) {
@jasdev
jasdev / AppDefault.swift
Last active Jun 14, 2019
Swift nonmutating Example
View AppDefault.swift
import Foundation
enum AppDefault {
case APIServer, runMode, homeDirectory, reactHost
var stringValue: String? {
get {
return NSUserDefaults.standardUserDefaults().stringForKey(String(self))
}
View SampleViewController.swift
final class SampleViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .System)
// ...
button.addTarget(self, action: #selector(SampleViewController.buttonTapped(_:)), forControlEvents: .TouchUpInside)
View SampleViewController.swift
final class SampleViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .System)
// ...
button.addTarget(self, action: #selector(SampleViewController.buttonTapped(_:)), forControlEvents: .TouchUpInside)
@jasdev
jasdev / Operation.swift
Created Feb 15, 2016
NSOperation Subclass with KVO Notifications
View Operation.swift
class Operation: NSOperation {
override var asynchronous: Bool {
return true
}
private var _executing = false {
willSet {
willChangeValueForKey("isExecuting")
}
@jasdev
jasdev / CustomCell.swift
Last active Jul 6, 2018
An approach to safer UITableViewCell and UICollectionViewCell reuse
View CustomCell.swift
class CustomCell: UITableViewCell, Reusable {
class var reuseIdentifier: String {
return "customCell"
}
}
class SupaHotCustomCell: CustomCell {
override class var reuseIdentifier: String {
return "supaHotCustomCell"
}
You can’t perform that action at this time.