title: "xx" is get-only property - How to Assign decoded Json Data to class (nested view model) Swift
body: <p>I have a watchapp that receives parent view model (nested view model) data from iPhone via json/file transfer. I am using &quot;Communicator&quot; library by @KaneCheshire.</p>
  <p>The data flow is</p>
  <ol>
  <li>Watch App launches - loading empty Parent View Model (<code>ReceivedDayProgViewModel</code>) into the app and showing activity indicator in <code>WatchLoadingView</code>.</li>
  <li><code>WatchLoadingView</code> set-up observer to receive actual json data/file from iPhone</li>
  <li><code>WatchLoadingView</code>'s observer receives actual json data, decodes them into ParentViewModel object (<code>ReceivedDayProgViewModel</code>)</li>
  </ol>
  <p>After step 3, I am having difficulty actually assigning the decoded object to the initially empty Parent View Model (<code>ReceivedDayProgViewModel</code>). There is a error message saying &quot;<code>**Cannot assign to property: 'receivedDayProgramVM' is a get-only property**</code>&quot;</p>
  <p>So I did look up the error and have an idea that my parent View Model is actually a &quot;computed-property&quot; instead of stored property - <a href="https://stackoverflow.com/questions/34071428/how-do-i-resolve-this-build-issue-cannot-assign-to-property-date-is-a-get-o">How do I resolve this build issue - cannot assign to property: &#39;date&#39; is a get only property</a> <strong>But I am still struggling to add codes to ensure &quot;set/write&quot; function is embedded in the Parent View Model(<code>ReceivedDayProgViewModel</code>).</strong></p>
  <p>Would appreciate any guidance/advice on how I can change my code to allow &quot;set/write&quot; operation in Parent View Model (<code>ReceivedDayProgViewModel</code>) so below step can be fulfilled.</p>
  <ol start="4">
  <li>Assign decoded object (<code>let ReceivedDayProgViewModel = try! decoder.decode(ReceivedDayProgViewModel.self, from: ReceivedDayProgVMData)</code>) (with values/data from iPhone) into the <code>@StateObject</code> in <code>ContentView</code>  (<code>receivedDayProgramVM</code>)</li>
  </ol>
  <p>Parent View Model</p>
  <pre><code>import Foundation
  import Combine
  
  class ReceivedDayProgViewModel: ObservableObject, Codable {
      
      @Published var workoutModel = WorkoutModel()
      @Published var watchDayProgramSequence: Int = 0
      @Published var exerciseVM: [WatchExerciseViewModel] = []
      
      enum CodingKeys: String, CodingKey {
          
          case dayProgramSeq
          case exerciseVM
          
      }
      
      init() {
             //initiliazed empty view model via this function
      }
      
      required init(from decoder: Decoder) throws {
          let container = try decoder.container(keyedBy: CodingKeys.self)
          
          watchDayProgramSequence = try container.decode(Int.self, forKey: .dayProgramSeq)
          exerciseVM = try container.decode([WatchExerciseViewModel].self, forKey: .exerciseVM)
          
          do {
              watchDayProgramSequence = try container.decode(Int.self, forKey: .dayProgramSeq)
          } catch DecodingError.typeMismatch {
              do {
                  watchDayProgramSequence = try Int(container.decode(String.self, forKey: .dayProgramSeq)) ?? 0
              }
          }
          
      }
      
      func encode(to encoder: Encoder) throws {
          var container = encoder.container(keyedBy: CodingKeys.self)
          
          try container.encode(watchDayProgramSequence, forKey: .dayProgramSeq)
          try container.encode(exerciseVM, forKey: .exerciseVM)
  
      }//encode function ends
      
  }
  </code></pre>
  <p>WatchApp</p>
  <pre><code>import SwiftUI
  import Communicator
  
  @main
  struct WatchApp: App {
  
      @SceneBuilder var body: some Scene {
          WindowGroup {
              NavigationView {
                  ContentView(selectedTab: 0, receivedDayProgramVM: ReceivedDayProgViewModel())
              }
          }
  
          WKNotificationScene(controller: NotificationController.self, category: &quot;myCategory&quot;)
      }
  }
  
  </code></pre>
  <p>ContentView</p>
  <pre><code>import SwiftUI
  import WatchKit
  import Combine
  import Communicator
  
  struct ContentView: View {
      @State var selectedTab = 0
      
      @StateObject var receivedDayProgramVM = ReceivedDayProgViewModel()
      
      var body: some View {
          
          NavigationView {
              
              //Show WatchLoadingView, if dayprogramVM is being fetched
              if receivedDayProgramVM.exerciseVM.count == 0 {
                  
                     WatchLoadingView()
                      .onAppear {
                          //Add Communicator functions if required
                           Blob.observe { Blob in
  
                               if Blob.identifier == &quot;dayProgramData&quot; {
  
                                   let ReceivedDayProgVMData = Blob.content
                                   print(&quot;WatchConnectivity blob setData printing \(ReceivedDayProgVMData)&quot;)
  
                                   let decoder = JSONDecoder()
                                   
                                   let ReceivedDayProgViewModel = try! decoder.decode(ReceivedDayProgViewModel.self, from: ReceivedDayProgVMData)
                                   
                                   //Assign decoded class(parent view model) to environment object in contentView
  //This is the line that causes error showing receivedDayProgramVM is get-only property
                                   receivedDayProgramVM = ReceivedDayProgViewModel
  
                                   
                               } //Blob for &quot;dayProgramData&quot; code ends
                               
                               
                           } //Blob observation code ends
                      } else {
                  
                  TabView(selection: $selectedTab) {
                      
                      WatchControlView().id(0)
                      SetInformationView().id(1)
                      SetRestDetailView().id(2)
                      //Check if WatchControlView can be vertically scrollable
                      NowPlayingView().id(3)
                  }
                  .environmentObject(receivedDayProgramVM)
              }
  
          }
      }
  }
              
  </code></pre>
  <p>WatchLoadingView</p>
  <pre><code>import SwiftUI
  import Communicator
  
  struct WatchLoadingView: View {
      
      var body: some View {
          
          GeometryReader { geometry in
              
              let rect = geometry.frame(in: .global)
           
              VStack {
                  ProgressView()
                  Text(&quot;Fetching Data...&quot;)
                      .font(.body)
              }
          }
      }
  }
  </code></pre>