Skip to content

Instantly share code, notes, and snippets.

/*
We are removing a product before listing all products so we don't expect to print 3 items.
Since the queue will be executed concurrently, there's no guarantee which task will execute first.
*/
func withoutBarrier() {
// We will sometimes see a list of 3 items instead of 2
for i in 1...1000 {
var resources = ["Product A", "Product B", "Product C"]
imageView.layer.cornerRadius = imageView.bounds.width/2
imageContainerView.layer.masksToBounds = false
imageContainerView.layer.cornerRadius = imageContainerView.bounds.width/2
imageContainerView.layer.shadowColor = UIColor.black.cgColor
imageContainerView.layer.shadowOpacity = 0.3
imageContainerView.layer.shadowRadius = 2
imageContainerView.layer.shadowOffset = .zero
import UIKit
class SegmentedCircleView: UIView {
private var piMultipliers: [(Double,Double)] {
switch enablers.count {
case 1:
return [(3/2, 4)]
case 2:
return [(3/2, 1/2), (1/2, 3/2)]
default:
@staticVoidMan
staticVoidMan / Dynamic.swift
Created February 17, 2022 11:48
Custom observable object with basic bind/fire
import Foundation
class Dynamic<T> {
typealias Listener = (T) -> Void
private var listener: Listener?
private var thread: RunLoop = .current
var value: T {
didSet {
//Ref: https://medium.com/@johnsundell/writing-unit-tests-in-swift-playgrounds-9f5b6cdeb5f7
import XCTest
class ExampleTests: XCTestCase {
func textExample1() {
XCTAssertEqual(2 * 2, 4)
}
/*
Ref:
- https://stackoverflow.com/q/44195986/2857130
- https://useyourloaf.com/blog/variable-height-table-view-header/
*/
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if let headerView = tableView.tableHeaderView {
@staticVoidMan
staticVoidMan / URLComponent+Fragments.swift
Created October 27, 2021 09:00
URLComponents Extension on fragments as a dictionary
extension URLComponents {
var fragments: [String:String]? {
return fragment?
.split(separator: "&")
.reduce(into: [:]) { partialResult, fragment in
let parts = fragment.split(separator: "=")
let key = String(parts[0])
let value = String(parts[1])
partialResult?[key] = value
}
protocol SomeProvider {
func getSomething(completion: @escaping (Result<Any,Error>)->Void)
}
extension SomeProvider {
func fallback(to provider: SomeProvider) -> SomeProvider {
SomeFallbackProvider(initial: self, fallback: provider)
}
}
@staticVoidMan
staticVoidMan / Helpers.swift
Created September 13, 2021 07:21
Generic Helpers
// Prevent `print` in Release mode
func print(_ args: Any..., separator: String = " ", terminator: String = "\n") {
#if DEBUG
let output = args.map(String.init(describing:)).joined(separator: separator)
print(output, terminator: terminator)
#endif
}
@staticVoidMan
staticVoidMan / problem.swift
Created August 20, 2021 05:52
SwiftUI | Problem | Buttons in a list row hierarchy
//Ref: https://stackoverflow.com/q/56561064/2857130
struct ItemListView: View {
var body: some View {
VStack {
List(0..<3) { section in
ItemOption(section: section)
.padding()
}
}