Skip to content

Instantly share code, notes, and snippets.

View mhijack's full-sized avatar
🎯
Focusing

Jianyuan Chen mhijack

🎯
Focusing
View GitHub Profile
struct Book {
var name: String
}
class BookStore: ObservableObject {
@Published var books: [Book] = [Book(name: "The Great Influenza")]
public func addBook(name: String) {
self.books.append(Book(name: name))
}
struct ContentView: View {
@State var dataStore = ["A", "B", "C"]
var body: some View {
HStack {
ChildViewA(characters: dataStore)
Spacer()
ChildViewB(characters: dataStore)
struct ContentView: View {
@State private var cannotProceed = false
var body: some View {
VStack {
Button("Toggle cannotProceed") {
cannotProceed.toggle()
}
Button("Proceed") {
import Combine
class LoginViewModel {
public var username = CurrentValueSubject<String, Never>("")
public var password = CurrentValueSubject<String, Never>("")
public var confirmPassword = CurrentValueSubject<String, Never>("")
public var isValid = false {
didSet {
print("Inputs are valid: \(isValid)")
import Combine
publisher1.combineLatest(publisher2)
import Combine
let integerConvertSubject = PassthroughSubject<String, Never>()
let integerConvertSubscription = integerConvertSubject
.compactMap({ value in
// Int initializer returns an optional
return Int(value)
})
.sink { value in
print("value came out of compact map: \(value)")
import Combine
let subscription1 = [0, 1, 1].publisher
.removeDuplicates()
.sink { value in
print(value)
}
// 0
// 1
import Combine
let intList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
intList.publisher
.filter({ $0 % 2 == 0 })
.map({ String("Received integer: \($0)") })
.sink { result in
print(result)
}
import Combine
class MapDemo {
var text: String = "" {
didSet {
print(text)
}
}
}
import Combine
let currentvalue = CurrentValueSubject<Int, Never>(0)
print(currentvalue.value) // prints: 0
currentvalue.value = 10
print(currentvalue.value) // prints: 10
let currentvalueSubscription = currentvalue.sink { value in
print(value)
}