Skip to content

Instantly share code, notes, and snippets.

//
// ContentView.swift
// Layout
//
// Created by Matt Gallagher on 7/6/19.
// Copyright © 2019 Matt Gallagher. All rights reserved.
//
import SwiftUI
@GeekAndDad
GeekAndDad / AppDelegate.swift
Created September 23, 2019 18:37
Xcode 11 and iOS 12 project - Edited source files after making the changes outlined in this blog post: https://geekanddad.wordpress.com/2019/09/23/making-ios-12-projects-in-xcode-11
//
// AppDelegate.swift
// make1012work
//
// Created by Dad on 9/23/19.
// Copyright © 2019 Geek & Dad, LLC. All rights reserved.
//
import UIKit
@GeekAndDad
GeekAndDad / AnyEncodable.swift
Last active October 28, 2019 22:35
How to make an AnyEncodable protocol that allows you to pass any struct conforming to Encodable to a function.
import Foundation
// based on clever example from Nick Lockwood: https://gist.github.com/nicklockwood/833fabacbc4b2d11ae7c7d4752b8fd18
// I only needed Encodable so trimmed it down to just that.
protocol AnyEncodableValue: Encodable {}
extension AnyEncodableValue {
func encode(to container: inout SingleValueEncodingContainer) throws {
try container.encode(self)
@GeekAndDad
GeekAndDad / ContentView.swift
Last active November 17, 2019 05:08
Using UIViewController via the SwiftUI Environment
//
// ContentView.swift
// TestSwiftUI1
//
//
import SwiftUI
struct ContentView: View {
@Environment(\.viewController) private var viewControllerHolder: UIViewController?
// point free video 65 at the 14:50 point.
// https://www.pointfree.co/episodes/ep65-swiftui-and-state-management-part-1#t887
//
// This code has an issue whereby a navigation link cannot be activated twice unless
// the other navigation link is actived in between the two activations of a particular
// link.
import SwiftUI
struct ContentView: View {
@GeekAndDad
GeekAndDad / OpaqueTypesPlayground.swift
Created January 10, 2020 21:02
macOS playground playing with opaque types & protocols.
// paste into new macOS playground
// Xcode 11.3 on 10.15.2
import Cocoa
protocol InfoTypeProtocol {}
protocol Base {
associatedtype InfoType: InfoTypeProtocol
var info: InfoType { get set }
@GeekAndDad
GeekAndDad / View_printMessage_playground.swift
Last active January 23, 2020 19:17
Simple printMessage extension to SwiftUI's View struct
// from the blog post: https://geekanddad.wordpress.com/2020/01/23/snippet-swiftui-view-printmessage-extension/
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
var body: some View {
VStack {
TestView(label: "hello", idx: 22)
.font(.title)
@GeekAndDad
GeekAndDad / ContentView.swift
Last active September 25, 2021 00:02
From the blog post "Tiny Hints: a couple of Xcode 11.3 SwiftUI Preview tips"
//
// ContentView.swift
// PreviewsTips2020.01.24
// Updated 2021.09.24
//
// Dad
// @GeekAndDad
//
// Code from blog post:
// https://geekanddad.wordpress.com/2020/01/24/tiny-hints-a-couple-of-xcode-11-3-swiftui-preview-tips/
@GeekAndDad
GeekAndDad / SampleView.swift
Last active February 13, 2020 23:13
Sample to show a blog writer a way to do what they wanted (lined up field values) that actually worked. Put this in an iOS app project and you can preview just this view and if you edit the text in the fields you can see it all adjust. Comment in the .border(Color.green) lines to see the frames of the Text views change dynamically as you type :)
//
// ContentView.swift
// bugtest
//
// Copyright © 2020 Geek & Dad, LLC All rights reserved.
//
import SwiftUI
struct PreferenceTest: View {
@GeekAndDad
GeekAndDad / ContentView.swift
Last active February 15, 2020 23:16
Quick hack Mac app to put up a sheet with multi-line text.
import SwiftUI
struct ContentView: View {
@State private var showSheet: Bool = false
@State private var name: String = "Default Name"
var body: some View {
VStack {
Text("Hello, World!")
.frame(maxWidth: .infinity, maxHeight: .infinity)