DataLife is an observable data storage class
- In ViewModel inherit from DataLifeViewModel and create a variable that will be the result value.
import DataLife
final class ViewModel: DataLifeViewModel {
var myName = DataLife<String>()
func fetchMyName() {
myName.value = "Darth Vader"
}
}
- In the ViewController you will call the variable that was created in the ViewModel that will be observed.
import UIKit
final class ViewController: UIViewController {
@IBOutlet weak var nameLabel: UILabel!
private let viewModel = ViewModel()
override func viewDidLoad() {
super.viewDidLoad()
setupState()
setupFetchMyName()
}
private func setupFetchMyName() {
viewModel.fetchMyName()
}
private func setupState() {
viewModel.myName.addObserver(viewModel) { [weak self] name in
self?.nameLabel.text = name
}
}
}
You see the demo here
import PackageDescription
let package = Package(
name: "<Your Product Name>",
dependencies: [
.package(url: "https://github.com/heroesofcode/DataLife", .upToNextMajor(from: "2.0.0"))
],
targets: [
.target(
name: "<Your Target Name>",
dependencies: ["DataLife"]),
]
)
To contribute, just fork this project and then open a pull request, feel free to contribute, bring ideas and raise any problem in the issue tab.
DataLife is released under the MIT license. See LICENSE for details.