Skip to content

Instantly share code, notes, and snippets.

View Gernot's full-sized avatar

Gernot Poetsch Gernot

View GitHub Profile
@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
@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 / 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 / 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 / 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 / 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 / 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!"
}
}
struct CharacteristicValue {
let format: Int8
let exponent: Int8 = -1
let unit: UInt16
let namespace: Int8
let descriptionInfo: Int16
}
var value = CharacteristicValue(format: 14, unit: 0x272F, namespace: 1, descriptionInfo: 0)
import Foundation
func doStuff<T:CollectionType where T.Generator.Element: Equatable, T.Index.Distance == Int>(collection:T) {
//Do Stuff
}
var list: [Any] = [1,2,2,3,4,5] //This HAS to be saved as [Any]. Or is there another way to specify all kinds of collections that would work with doStuff()?
doStuff(list) //This fails: cannot invoke 'doStuff' with an argument list of type '([Any])'
/// Is there any way to use the Options from libxml2 with the cool new `OptionSet` Syntax from Swift 2?
//This is how I do it now
let options = Int32(HTML_PARSE_RECOVER.rawValue | HTML_PARSE_NOWARNING.rawValue | HTML_PARSE_NOERROR.rawValue)
// ideally I want this, but it doesn't work
let options: htmlParserOption = [HTML_PARSE_RECOVER, HTML_PARSE_NOWARNING, HTML_PARSE_NOERROR]
//here's the typedef from libxml/HTMLparser.h
/*