Skip to content

Instantly share code, notes, and snippets.

@0xWDG
Created October 10, 2021 18:58
Show Gist options
  • Save 0xWDG/52a78f37d1475a2239ff704176e5742a to your computer and use it in GitHub Desktop.
Save 0xWDG/52a78f37d1475a2239ff704176e5742a to your computer and use it in GitHub Desktop.
SwiftUI Binding
//
// ContentView.swift
// SwiftUITest
//
import SwiftUI
class ClassWithVariables {
var intVar: Int
var strVar: String
var booVar: Bool
public static let shared = ClassWithVariables()
init() {
intVar = 1
strVar = "Hello"
booVar = true
}
}
struct ContentView: View {
var cls = ClassWithVariables.shared
// Placeholder.
@State private var updText = Binding(
get: { "" },
set: { _ in }
)
// For alert view controller.
@State private var displayFromMemory = false
// could be directly, if we dont use `cls`
func afterInit() {
updText = Binding.init(
get: {
self.cls.strVar
},
set: { newValue in
// This will be called more times
// Should not be a problem.
// This is an example log.
// the last 2 updates,
// removes the last character..
//
// NewVal="Hello,doestiswork"
// NewVal="Hello,doestiswork"
// NewVal="Hello,doestiswor"
// NewVal="Hello,doestiswor"
print("NewVal=\"\(newValue)\"")
self.cls.strVar = newValue
}
)
}
var body: some View {
VStack {
Text(
"Hello, world!"
).padding()
TextField(
"Enter Test",
text: updText // <- will never updated?
)
.multilineTextAlignment(.center)
.border(.blue, width: 1)
.padding()
Button("Load text from memory") {
displayFromMemory = true
}
.alert(isPresented: $displayFromMemory) {
Alert(
title: Text("TEXT"),
message: Text("\"\(cls.strVar)\""),
dismissButton: .default(
Text("OK")
)
)
}
.onAppear(
perform: afterInit
)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
//
// ContentView.swift
// SwiftUITest
//
import SwiftUI
class ClassWithVariables {
var intVar: Int
var strVar: String
var booVar: Bool
public static let shared = ClassWithVariables()
init() {
intVar = 1
strVar = "Hello"
booVar = true
}
}
struct ContentView: View {
@State private var updText = Binding.init(
get: {
ClassWithVariables.shared.strVar
},
set: { newValue in
// This will be called more times
// Should not be a problem.
// This is an example log.
// the last 2 updates,
// removes the last character..
//
// NewVal="Hello,doestiswork"
// NewVal="Hello,doestiswork"
// NewVal="Hello,doestiswor"
// NewVal="Hello,doestiswor"
print("NewVal=\"\(newValue)\"")
ClassWithVariables.shared.strVar = newValue
}
)
// For alert view controller.
@State private var displayFromMemory = false
var body: some View {
VStack {
Text(
"Hello, world!"
).padding()
TextField(
"Enter Test",
text: updText // <- will never updated?
)
.multilineTextAlignment(.center)
.border(.blue, width: 1)
.padding()
Button("Load text from memory") {
displayFromMemory = true
}
.alert(isPresented: $displayFromMemory) {
Alert(
title: Text("TEXT"),
message: Text("\"\(ClassWithVariables.shared.strVar)\""),
dismissButton: .default(
Text("OK")
)
)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
@0xWDG
Copy link
Author

0xWDG commented Oct 10, 2021

import SwiftUI

class myClass {
    // This one needs also to be updated.
    public var score = 0

    init () { }

    public func updateAndPrint(score: Int) {
        self.score = score
        print("Score: \(score)")
    }
}

struct ContentView: View {
    @State var score: Int = 0
    var a = myClass()

    var body: some View {
        VStack {
            Text("Your score is \(score)")
            Button("Increase Score") {
                score += 1
                a.updateAndPrint(score: score)
            }
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment