Observer and Struct


How to use self keyword inside NotificationCenter.default.addObserver method? I need to call a struct's function (using self keyword) inside this init() block when observer is executed

Mutating a struct out-of-line doesn't make much sense. struct generally doesn't have a sense of identity. Swift doesn't know which struct to be mutated, worse if it is a let struct.

It's more appropriate use class instead.

how to make a ContentView class?

View's in SwiftUI already use its location in the view hierarchy for identification. It's ill-advised to make one a class since it'd easily have two competing notions of the same view. Doubly more so that View shouldn't even mutate.

Instead, you should get a publisher from NotificationCenter to use with
one of the View's completion handler, specifically onReceive.

If you don't have time to look up how to use combine, it should look something similar to this:

let commonPublisher = NotificationCenter.publisher(for: NSNotification.Name.AVPlayerItemDidPlayToEndTime)

struct ContentView: View {
  var body: some View {
    Text("")
      .onReceive(commonPublisher) {
        self.someFunc()
      }
  }
}

Even then, it shouldn't be a mutating function. You should only change the State of the view, or the attached ObservableObject, none of which requires you to mutate the struct. You shouldn't mark any View function as mutating essentially.

thanks a lot