NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

ObservableObject in MacOS

Forums > macOS


I have written a iOS App and it works very well.

When a user changes a Toggle within a Settings View, the ContentView List also updates no problems.

For example: When showing a list of data, the user has the option to not show a field like say a date, or the name, etc.. by simply opening a Settings view and Toggling a switch to off, so not to display the field.

So when the user toggles it Switch in Settings the List within the ContentView changes and the field that they wish to show or not show updates, straight away !! Wooo hooo.

However... Yep.. However..

Using the same code within my Mac App (Mac only App) it reads the same data from iCloud and updates the Coredata with the list.. just fine there.

I have three View in one Window for the Mac App, the left side is the list, the right side is the entry for new names etc and at the bottom of the screen is a couple of Switches within the Settings View.

I was expecting seeing that I was using the same code as the ios app that all would work.. The Mac App Builds and Runs, but !

When I toggle the Switch to say not display a name field, it does not update the List within the ContentView List.

The only way it works is to shut down the app an reload it, then the name field within the List is not showing which is correct but I expected that to work as soon as I click on the Check box (Toggle)

Some of my code: Sorry about my messy code.. I'm an Old Newby !!

Cheers Craig.

import SwiftUI import Foundation

class SettingsViewModel: ObservableObject {

@Published var showName: Bool = UserDefaults.standard.bool(forKey: "showName") { didSet { UserDefaults.standard.set(self.showName, forKey: "showName") }}

import SwiftUI

struct Settings: View {

@ObservedObject var settingsVM = SettingsViewModel()

var body: some View { VStack { Divider() HStack {

Label("Settings ", systemImage: "gear") .font(.headline) .padding(.leading, 10) Spacer() } HStack{

Toggle(isOn: self.$settingsVM.showName) { Text("Show Name in List:") }.padding(.leading, 10).padding(.trailing, 10)

} .padding(.leading, 10) Spacer() }

} } }

import SwiftUI import CoreData

struct ContentView: View { @Environment(.managedObjectContext) var viewContext

@FetchRequest(entity: Stuff.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Stuff.timeStamp, ascending: false)], animation: .default) private var items: FetchedResults<Stuff>

@ObservedObject var settingsVM = SettingsViewModel()

var body: some View {


  HStack(alignment: .center){
      //  Spacer()

      List {
        ForEach(items, id: \.self) { item in

          HStack(alignment: .center) {

            Text(item.callsign ?? "Empty")
              .frame(width: 100.0, alignment: .topLeading)

            if settingsVM.showName == true {
            Text( ?? "Empty")
              .frame(width: 70.0, alignment: .topLeading)

            Text(item.location ?? "Empty")
              .frame(width: 120.0, alignment: .topLeading)

            Text( ?? "Empty")
              .frame(width: 70.0, alignment: .topLeading)

              .frame(width: 40.0, alignment: .topLeading)

              .frame(width: 40.0, alignment: .topLeading)

            if item.isFavourite == true {
              //frame(width: 40.0, alignment: .topLeading)

        .onDelete(perform: deleteItems)
      .frame(width: nil, height: 400)





Hacking with Swift is sponsored by MadMachine

SPONSORED Want to try Swift on microcontrollers? MadMachine provides ways to interact with the physical world in a Swift way. Join us and have fun!

Get it now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.