Skip to content

Instantly share code, notes, and snippets.

View GreatApe's full-sized avatar

Gustaf Kugelberg GreatApe

View GitHub Profile
@GreatApe
GreatApe / Observing.swift
Last active March 10, 2023 07:10
Observing dependencies
switch action {
case .task:
return .observe {
myDependency.events => Action.onEvent
}
}
// where this is put at the end of the View:
.task {
@GreatApe
GreatApe / ObserveDependency.swift
Created February 20, 2023 09:35
Observe Dependency
struct TestReducer: Reducer {
@Dependency(\.repo) var repo
struct State: Equatable {
var string: String = ""
var int: Int = 0
}
enum Action {
case task
import SwiftUI
import Dependencies
import Combine
import ComposableArchitecture
struct ContentView: View {
private let myStore: StoreOf<MyReducer> = Store(initialState: .init(number: 1), reducer: MyReducer())
private let otherStore: StoreOf<OtherReducer> = Store(initialState: .init(number: 2), reducer: OtherReducer())
var body: some View {
@GreatApe
GreatApe / GenericSegmentationFault.swift
Last active August 7, 2021 11:04
Generic segmentation fault
// TestType - constrained to built-in protocol, uses conditional conformance (a)
struct TestType<T: Error> { }
// Conditional conformance (b)
extension Array: Error where Element: Error { }
struct GenericType<G> {
// FAILS: These both segmentation fault (c)
func test<T>(_ value: TestType<[T]>) { }
// Works
struct TestView1: View {
var body: some View {
SmartView {
VStack {
Text("random text")
Text("More random text")
}
}
}