Published object not publishing, am I doing it wrong? - ios

My code looks like this:
final class MyModelController: ObservableObject {
#Published var model = MyModel()
}
enum ButtonSelection: Int {
case left, right
}
final class MyModel {
var buttonSelection: ButtonSelection?
}
I have injected an instance of MyModelController as an #EnvironmentObject into my SwiftUI views.
When I set myModelController.model.buttonSelection, I thought it would update myModelController.model and send out an update because it's marked as #Published. However, it doesn't. How can I fix this?

#Published only detects changes for value types. MyModel is a class, which is a reference type.
If possible, changing MyModel to a struct will fix this. However, if this is not possible, see the rest of this answer.
You can fix it with Combine. The below code will update MyModelController when model (now an ObservableObject) changes.
final class MyModelController: ObservableObject {
#Published var model = MyModel()
init() {
_ = model.objectWillChange.sink { [weak self] in
self?.objectWillChange.send()
}
}
}
/* ... */
final class MyModel: ObservableObject {
#Published var buttonSelection: ButtonSelection?
}

Related

Swift override var inheritance

I am trying to do something like this in Swift.
public class BaseModel {
}
public class SubModel:BaseModel {
}
public class BaseClass {
public var model:BaseModel
init(_ model:BaseModel) {
self.model = model
}
}
public class SubClass: BaseClass {
override var model:SubModel
}
But the complier is not allowing me to override model object with a subclass. Is it possible to achieve something like what I am trying to do above in Swift using inheritance?
As written, this wouldn't be type-safe. Your interface requires that subclass.model = model has to work for any model (and in this specific example, SubClass(model) also is "legal" for any model because it's currently inheriting the init).
What I believe you really mean is that all BaseClass can return a Model, but SubClass can only be set with a SubModel.
How you fix this depends heavily on what the users of SubClass look like and why you're reaching for inheritance. As a rule, you should be hesitant to reach for inheritance in Swift. It's fully supported, but Swift tends to prefer other tools than class inheritance.
A common solution for this specific example would be a generic, for example:
// Place any general Model requirements here.
public protocol BaseModel {}
// Just marking things final to emphasize that subclassing is not required
// These can all also be structs depending on if you need values or references
public final class SubModel: BaseModel {}
public final class BaseClass<Model: BaseModel> {
var model: Model
init(_ model: Model) {
self.model = model
}
}
// You can typealias specific instances if that helps
// With this, the syntax is extemely close to what you were trying to do
typealias SubClass = BaseClass<SubModel>
let sc = SubClass(SubModel())
let model: BaseModel = sc.model
// But, it's type safe
public final class OtherModel: BaseModel {}
sc.model = OtherModel // Cannot assign value of type OtherModel to type SubModel
let bad = SubClass(OtherModel()) // Cannot convert value of type 'OtherModel' to expected argument type 'SubModel'
If BaseClass and SubClass were more complex, and had more internal logic to them, then you could move up to protocols for these, but it would depend on the particular problem you were solving. I'd generally start with generics for the situation you're describing.
You cannot change the types of stored properties in Swift. But covariant overrides are fine for methods and computed properties. So as long as you make model a computed property, you can use inheritance here, but you must be very careful when doing this to avoid crashes.
The simplest approach is to just add a new property with its own name to SubClass:
var subModel: SubModel { model as! SubModel }
But to get the overriding behavior you're asking for, you need to make model a computed property:
public class BaseClass {
private var _model: BaseModel
public var model: BaseModel { _model }
init(_ model:BaseModel) {
self._model = model
}
}
Then you can override model in SubClass:
public class SubClass: BaseClass {
public override var model: SubModel { super.model as! SubModel }
init(_ model: SubModel) {
super.init(model)
}
}
But note that this is dangerous. It is possible for BaseClass or a subclass of SubClass to break the invariant, and then this will crash. To fix that, you should make _model a let value, and make SubClass final:
public class BaseClass {
private let _model: BaseModel
public var model: BaseModel { _model }
init(_ model:BaseModel) {
self._model = model
}
}
public final class SubClass: BaseClass {
public override var model: SubModel { super.model as! SubModel }
init(_ model: SubModel) {
super.init(model)
}
}
All of this is awkward and hard to keep correct. It's hard to keep class inheritance correct in all OOP languages, and that leads to a lot of bugs. That's why Swift encourages other tools, like generics, to solve these problems. They're much easier to write correctly, and the compiler can catch your mistakes.

Swift subclasses complex subtype

I have this design problem in Swift. Here is my root class and it's subclasses:
enum VisualItemType {
case video, picture, text,...
}
class VisualItem {
public var itemType: VisualItemType
...
}
class PictureItem:VisualItem {
private var subItems:[PictureItem OR TextItem But NOT VideoItem]? //Overlay of picture can be picture or text but not video
}
class VideoItem: VisualItem {
private var subItems:[VisualItem]? //Overlay of video can be any type of visual item
}
How do I cleanly implement subItems of PictureItem in Swift without creating a new enum of subtypes?
You can do that by introducing a new protocol that only TextItem and PictureItem will conform:
protocol PictureOverlayable: VisualItem {}
class TextItem: VisualItem, PictureOverlayable {}
class PictureItem: VisualItem, PictureOverlayable {
private var subItems: [PictureOverlayable]?
}

How do I get get set to work with protocols?

I'm confused when using get set in protocols. Using only get works fine, but the set part doesnt'.
protocol MainViewModelProtocol {
var localDoor: LocalDoorCoreDataObject { get set }
}
extension MainViewModelProtocol {
var localDoor: LocalDoorCoreDataObject {
get { return MainViewModel.instance.localDoor }
set { localDoor = newValue }
}
}
final class MainViewModel: MainViewModelProtocol {
var localDoor: LocalDoorCoreDataObject = LocalDoorCoreDataObject()
...
Then when I use it in the viewController
self.mainViewModel.localDoor = $0
But this gives me the error
Cannot assign to property: 'mainViewModel' is a get-only property
How do I set it up properly?
EDIT
Initiation of the viewModel is done with factory based dependency injection
protocol MainViewModelInjected {
var mainViewModel: MainViewModelProtocol { get }
}
extension MainViewModelInjected {
var mainViewModel: MainViewModelProtocol { return MainViewModel.instance }
}
It is totally depends on how you create object for mainViewModel.
Let's create some cases with your code:
import UIKit
typealias LocalDoorCoreDataObject = String
protocol MainViewModelProtocol {
var localDoor: LocalDoorCoreDataObject { get set }
}
extension MainViewModelProtocol {
var localDoor: LocalDoorCoreDataObject {
get { return MainViewModel.instance.localDoor }
set { localDoor = newValue }
}
}
final class MainViewModel: MainViewModelProtocol {
static let instance = MainViewModel()
var localDoor: LocalDoorCoreDataObject = LocalDoorCoreDataObject()
}
protocol MainViewModelInjected {
var mainViewModel: MainViewModelProtocol { get }
}
extension MainViewModelInjected {
var mainViewModel: MainViewModelProtocol { return MainViewModel.instance }
}
Case 1
Here we are creating an object and assigning object through getter as a closure.
So, here mainViewModel has only getter not setter i.e. it'a get-only property.
var mainViewModel: MainViewModelProtocol { MainViewModel.instance }
mainViewModel.localDoor = "assign some thing" // Error: Cannot assign to property: 'mainViewModel' is a get-only property
Case 2
Here we are directly assigning object to mainViewModelOther. So, this will be a normal property and you can make changes in properties of model.
var mainViewModelOther: MainViewModelProtocol = MainViewModel.instance
mainViewModelOther.localDoor = "assign some thing"
Case 3
You can also create a class that will hold your model object, and created another object of your class. You can make changes in properties of model.
class MyClass {
var mainViewModel: MainViewModelProtocol = MainViewModel.instance
}
let myClassObj = MyClass()
myClassObj.mainViewModel.localDoor = "assign some thing"
TL;DR
Mark your MainViewModelProtocol as being class-only (i.e. protocol MainViewModelProtocol: class { ... }) to solve the issue.
The long answer
To understand why marking your MainViewModelProtocol as class-only fixes the problem, we need to take couple steps back and look at how structs and classes are stored internally.
Case 1: MainViewModelProtocol is a reference-type (i.e. class)
First, let's consider the case where MainViewModel is a class: Classes are reference-types, which means that after you retrieve the your view model through the mainViewModel property, you have a pointer to the same view model that is stored inside your view controller. Modifying the referenced type will also modify the view model of the view itself (since they both point to the same object). As an example
/* ... */
class MainViewModel: MainViewModelProtocol { /* ... */ }
var viewModel = myViewController.mainViewModel
viewModel.localDoor = /* something */
modifies the view model that's shared between the local variable viewModel and the view controller. This is exactly what you want.
Case 2: MainViewModelProtocol is a value type (i.e. struct)
Now let's consider if the MainViewModel was a struct: structs are value-types, so retrieving the view model through the mainViewModel computed property essentially clones the view model. Now you might modify the retrieved view model as much as you like locally, but there is no way assign it back to your view controller
/* ... */
struct MainViewModel: MainViewModelProtocol { /* ... */ }
var viewModel = myViewController.mainViewModel
viewModel.localDoor = /* something */
just modifies the local copy of the view model stored in the viewModel variable. There is no way to assign the local variable back to myViewController.
Conclusion
I hope this illustrates why your pattern only works with reference-types and not value types.
Now the Swift compiler needs to be conservative and consider both cases since it doesn't know if all types conforming to MainViewModelProtocol will be classes or structs (consider public protocols vended as a library to which library-users can conform). If you add the class-constraint to the protocol, you tell the compiler that using the pattern from Case 1 is totally fine – just grab a shared instance and modify it – and that there is no need for a setter to modify the view model.
No need to mark MainViewModelProtocol as class only, when the compiler says :
Cannot assign to property: 'mainViewModel' is a get-only property
it's actually complaining about your view controller implementation. I assume mainViewModel is a computed property so you can't assign it.
I managed to reproduce your error with the following playground :
typealias LocalDoorCoreDataObject = String
protocol MainViewModelProtocol {
var localDoor: LocalDoorCoreDataObject { get set }
}
extension MainViewModelProtocol {
var localDoor: LocalDoorCoreDataObject {
get { return MainViewModel.instance.localDoor }
set { localDoor = newValue }
}
}
final class MainViewModel: MainViewModelProtocol {
static let instance = MainViewModel()
var localDoor: LocalDoorCoreDataObject = LocalDoorCoreDataObject()
}
final class FakeVC {
var mainViewModel: MainViewModelProtocol {
MainViewModel.instance
}
}
var viewController = FakeVC()
viewController.mainViewModel.localDoor = "foo" // Cannot assign to property: 'mainViewModel' is a get-only property
I got rid of the error by changing FakeVC implementation to :
final class FakeVC {
var mainViewModel: MainViewModelProtocol = MainViewModel()
}

#Published for a computed property (or best workaround)

I'm trying to build an app with SwiftUI, and I'm just getting started with Combine framework. My first simple problem is that I'd like a single variable that defines when the app has been properly initialized. I'd like it to be driven by some nested objects, though. For example, the app is initialized when the account object is initialized, the project object is initialized, etc. My app could then use GlobalAppState.isInitialized, instead of inspected each nested object.
class GlobalAppState: ObservableObject {
#Published var account: Account = Account()
#Published var project: Project = Project()
#Published var isInitialized: Bool {
return self.account.initialized && self.project.initialized;
}
}
I get the error Property wrapper cannot be applied to a computed property
So...clearly, this is currently disallowed. Is there a way I can work around this??? I'd like to be able to use GlobalAppState.initialized as a flag in the app. More to the point, something like GlobalAppState.project.currentProject, which would be a computed property returning the currently selected project, etc...
I can see this pattern being used in a thousand different places! Any help would be wildly appreciated...
Thanks!
In this case there's no reason to use #Published for the isInialized property since it's derived from two other Published properties.
var isInitialized: Bool {
return self.account.initialized && self.project.initialized;
}
Here is one case if both account and project are structures.
struct Account{
var initialized : Bool = false
}
struct Project{
var initialized : Bool = false
}
class GlobalAppState: ObservableObject {
#Published var account: Account = Account()
#Published var project: Project = Project()
#Published var isInitialized: Bool = false
var cancellabel: AnyCancellable?
init(){
cancellabel = Publishers.CombineLatest($account, $project).receive(on: RunLoop.main).map{
return ($0.0.initialized && $0.1.initialized)
}.eraseToAnyPublisher().assign(to: \GlobalAppState.isInitialized, on: self) as AnyCancellable
}
}
struct GlobalAppStateView: View {
#ObservedObject var globalAppState = GlobalAppState()
var body: some View {
Group{
Text(String(globalAppState.isInitialized))
Button(action: { self.globalAppState.account.initialized.toggle()}){ Text("toggle Account init")}
Button(action: { self.globalAppState.project.initialized.toggle()}){Text("toggle Project init")}
}
}
}

Make class type a Dictionary key (Equatable, Hashable)

Say I have a class named LivingCreature
And other classes that inherit from it:
Human
Dog
Alien
This is what I'm trying to accomplish:
let valueForLivingCreature = Dictionary<Alien, String>
And access it like so:
let alienValue = livingCreatureForValue[Alien]
But this means the class should conform to Equatable and Hashable, but the class itself, not the class instance.
I've tried various ways of accomplishing this, but no luck.
As a compromise I've came up with is:
typealias IndexingValue = Int
class LivingCreature {
static var indexingValue: IndexingValue = 0
}
And then I can use the class as a key like so:
let livingCreatureForValue = Dictionary<IndexingValue, String>
Access:
let alienValue = livingCreatureForValue[Alien.indexingValue]
But, this way the IndexingValue should be set per class, by hand.
I would like to make a hash from the class itself like so:
class LivingCreature {
static var indexingValue: IndexingValue {
return NSStringFromClass(self).hash
}
}
This is not possible because self is not accessible is static var.
My question is, is there a better way of addressing this kind of issue?
Edit:
#Paulw11 Asked me why not make LivingCreature confirm to Equatable and Hashable,
The reason is I would not be able to access the value by the class type reference.
I would have to alloc an instance every time:
let alienValue = livingCreatureForValue[Alien()]
I do not want to call "Alien()" every time for finding a value.
And the component that uses it, doesn't care about the livingCreature instance, only about the class type.
I assume your are trying something like:
let valueForLivingCreature = Dictionary<LivingCreature.Type, String>
and:
let alienValue = valueForLivingCreature[Alien.self]
Then you can use ObjectIdentifier:
class LivingCreature {
class var classIdentifier: ObjectIdentifier {
return ObjectIdentifier(self)
}
//...
}
class Human: LivingCreature {
//...
}
class Dog: LivingCreature {
//...
}
class Alien: LivingCreature {
//...
}
let valueForLivingCreature: Dictionary<ObjectIdentifier, String> = [
Human.classIdentifier: String(Human),
Dog.classIdentifier: String(Dog),
Alien.classIdentifier: String(Alien),
]
let alienValue = valueForLivingCreature[Alien.classIdentifier] //->"Alien"
But in most use cases when you want to use meta-class as a dictionary key, you can find another way around:
class LivingCreature {
class var classValue: String {
return String(self)
}
//...
}
class Human: LivingCreature {
//...
//Override `classValue` if needed.
}
class Dog: LivingCreature {
//...
}
class Alien: LivingCreature {
//...
}
let alienValue = Alien.classValue //->"Alien"

Resources