Skip to content

Instantly share code, notes, and snippets.

public extension Int {
public var seconds: DispatchTimeInterval {
return DispatchTimeInterval.seconds(self)
}
public var second: DispatchTimeInterval {
return seconds
}
@Thomvis
Thomvis / Migrated.swift
Last active January 30, 2024 16:11
An approach towards migrating properties in a Codable struct without having to write a custom Codable implementation
import Cocoa
// Say we have a Person model
enum V1 {
struct Person: Codable {
let name: String
var age: Int
}
}
//
// ContentView.swift
// NavigationViewTest
//
// Created by Thomas Visser on 04/11/2019.
// Copyright © 2019 Thomas Visser. All rights reserved.
//
import SwiftUI
struct ContentView: View {
var body: some View {
VStack(alignment: .customAlignment) {
HStack {
Text("100 m").font(Font.body.bold())
Text("Usain Bolt").setCustomAlignmentToLeading()
}
HStack {
Text("5 km").font(Font.body.bold())
Text("Joshua Cheptegei").setCustomAlignmentToLeading()
//
// ContentView.swift
// FlowLayoutST
//
// Created by Chris Eidhof on 22.08.19.
// Copyright © 2019 Chris Eidhof. All rights reserved.
//
import SwiftUI
struct FlowLayout {
https://open.spotify.com/playlist/5xdvmNizfHeCz8fVvaXL4F?si=1f_Q5o4KRh-63p-mwckUlg&dl_branch=1
@Thomvis
Thomvis / SwiftUI-Circular.swift
Last active August 4, 2021 21:41
Rough attempt at creating a container view that lays out its children in a circle #SwiftUI
struct ContentView: View {
@State var count: Int = 3
var body: some View {
return NavigationView {
VStack(spacing: 50) {
HStack {
Button(action: { self.count += 1 }) {
Text("Add")
}
// This view overrides safeAreaInsets in order to work around
// (possibly erroneous) behavior of UIKit where safe area insets change
// when a view is transformed. (see http://www.openradar.me/35532074)
//
// Use this view as the direct subview of the view that has the transform applied.
// This view's frame should be equal to the superview's bounds for this to work.
@objc public class TransformIgnoringSafeAreaInsetsView: UIView {
@available(iOS 11.0, *)
override public var safeAreaInsets: UIEdgeInsets {
guard let superview = self.superview, superview.transform != .identity else {
@Thomvis
Thomvis / NestedClosureWeakSelfRetainCycle.swift
Last active April 13, 2021 12:13
Capturing a weak reference to self in a nested closure could cause a retain cycle. `[weak self]` weakly captures self from the outer closure. Since the outer closure did not declare a weak reference to self, it will keep a strong one.
class ViewModel {
let intGetterGetter: () -> () -> Int?
var currentInt = 2
init() {
self.intGetterGetter = {
return { [weak self] in
return self?.currentInt
}
@Thomvis
Thomvis / FunctionView.swift
Created June 7, 2019 08:39
Function components are a thing in React. Here's what they look like in SwiftUI.
func ShieldIcon(ac: Int) -> some View {
ZStack {
Image(systemName: "shield")
.font(.title)
Text("\(ac)")
.font(.caption)
}
}