Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save karigrooms/62b6208a8e034e2bf0e73c86a6e15d41 to your computer and use it in GitHub Desktop.
Save karigrooms/62b6208a8e034e2bf0e73c86a6e15d41 to your computer and use it in GitHub Desktop.
Blog: ObservableObjects and Protocols - Protocol with @published
// Copyright 2021 Expedia, Inc.
// SPDX-License-Identifier: Apache-2.0
import Combine
protocol PriceRequesting: ObservableObject {
var price: Price? { get }
func fetch()
}
class PricingProvider: PriceRequesting {
private(set) var price: Price?
init(price initialValue: Price? = nil) {
self.price = initialValue
}
func fetch() {
// TODO: fetch pricing from a service
let price = Int.random(in: 42...150)
self.price = Price(amount: "$\(price)", description: "avg/night")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment