Skip to content

Instantly share code, notes, and snippets.

View Gernot's full-sized avatar

Gernot Poetsch Gernot

View GitHub Profile
@Gernot
Gernot / LocalizedStrings.swift
Created March 12, 2019 19:15
A proposal to have much nicer, backwards compatible localizable strings in Swift
/* Here is what I want to write, and what Xcode sees. */
struct Loc {
struct someDomain {
/// Here is some description of what this string is doing. It's readable in the AutoComplete Description!
static let myStringName = "Woah, woah, this is the actual content of the String!"
}
}
@Gernot
Gernot / TestView.swift
Created July 10, 2020 10:27
Scrollable Centered Text
import SwiftUI
struct TestView: View {
var body: some View {
ScrollView {
VStack {
Text("Text Before")
ScrollView(.horizontal) {
Text("Scrollable Text")
.border(Color.red)
@Gernot
Gernot / AirplaneChallenge.swift
Created September 10, 2020 11:29
SwiftUI Airplane Challenge
import SwiftUI
/**
Here's the challenge:
- The Airplane should usually be *centered in the cell*, so they are underneath each other in multiple cells.
- No city should overlap the the airplane. If so the airplane should move to the left or the right.
- If the space for the airplane is too small, it should not appear.
I know in UIKit this can easily be done with a handful of AutoLayout constraints and priorities.
But what is the way to do this in SwiftUI?
@Gernot
Gernot / Toggle.swift
Created October 16, 2020 16:40
Model with Properties form AppStorage
import Foundation
import SwiftUI
import Combine
import PlaygroundSupport
struct Model {
@AppStorage(wrappedValue: true, "Test") var isEnabled
}
struct MyView: View {
@Gernot
Gernot / Generic.swift
Created April 22, 2021 11:57
What is happening here?
import Foundation
class Foo<Value> {
init(value: Value) {
self.value = value
}
let value: Value
@Gernot
Gernot / Assignable.swift
Last active May 13, 2021 12:55
Assignable Extension on NSObject
import Foundation
import Combine
/**
I have two objects, Foo and Bar. Bar is a classic NSObject that hat a KVO observable Value thatchanges on an arbitrairy thread.
Foo is an ObservableObject with a published value that is derived from bar. That published value should change in the Main thread so SwiftUI does not complain.
However, if I do it with the Publisher/Combine solution only the initial value is nil, and the UI flickers. Instead I want to set the inital value in the init to the current value, and use the publisher only for new values and no longer for initial values.
@Gernot
Gernot / transition.swift
Created January 5, 2022 18:51
List like transitions for VStacks?
import Foundation
import SwiftUI
import PlaygroundSupport
let allTexts = ["one", "two", "three", "four", "five"]
let selectedTexts = ["two", "four"]
struct TestView: View {
@State private var expanded = false