0

I am using a library, which is more tailored towards Swift UI, but I am creating app based on regular Views and ViewControllers. The object for that library need to be declared(at least from my understanding) it as either @StateObject or @ObservableObject, I get the same error for both:

Accessing StateObject's object without being installed on a View. This will create a new instance each time.

From similar issues, the solution seems to be to use .onAppear(Accessing StateObject's object without being installed on a View. This will create a new instance each time - SwiftUI), but I am lost on how to call .onAppear for an @IBOutlet Is there a way to call .onAppear for a view, which is declared as:

@IBOutlet var hostView: CPTGraphHostingView!

?

UPDATE

If I declare the object as

var conductor = TunerConductor()

The Tuner Conductor errors out with the same throw in the initialization phase:

  init() {
    print("init started ")
    guard let input = engine.input else { fatalError() }

    guard let device = engine.inputDevice else { fatalError() }
    print("input selected")
    initialDevice = device

    mic = input
    tappableNodeA = Fader(mic)
    tappableNodeB = Fader(tappableNodeA)
    tappableNodeC = Fader(tappableNodeB)
    silence = Fader(tappableNodeC, gain: 0)
    engine.output = silence
    print("objects init")
    tracker = PitchTap(mic) { pitch, amp in
        DispatchQueue.main.async {
            self.update(pitch[0], amp[0])
        }
    }
    
}

Output looks like:

init started

2022-04-22 17:53:21.604952-0700 SING[9300:646252] throwing -10878 2022-04-22 17:53:21.606254-0700 SING[9300:646252] throwing -10878 2022-04-22 17:53:21.606352-0700 SING[9300:646252] throwing -10878 2022-04-22 17:53:21.606437-0700 SING[9300:646252] throwing -10878 2022-04-22 17:53:21.606514-0700 SING[9300:646252] throwing -10878 2022-04-22 17:53:21.606606-0700 SING[9300:646252] throwing -10878 2022-04-22 17:53:21.606685-0700 SING[9300:646252] throwing -10878 2022-04-22 17:53:21.606764-0700 SING[9300:646252] throwing -10878 2022-04-22 17:53:21.606839-0700 SING[9300:646252] throwing -10878

input selected

2022-04-22 17:53:21.615502-0700 SING[9300:646252] throwing -10878 2022-04-22 17:53:21.615950-0700 SING[9300:646252] throwing -10878 2022-04-22 17:53:21.616028-0700 SING[9300:646252] throwing -10878 2022-04-22 17:53:21.616097-0700 SING[9300:646252] throwing -10878 2022-04-22 17:53:21.616167-0700 SING[9300:646252] throwing -10878 2022-04-22 17:53:21.616237-0700 SING[9300:646252] throwing -10878 2022-04-22 17:53:21.616320-0700 SING[9300:646252] throwing -10878 2022-04-22 17:53:21.616396-0700 SING[9300:646252] throwing -10878 2022-04-22 17:53:21.616471-0700 SING[9300:646252] throwing -10878

objects init

There are no mentions of this error code in the library nor in swift in general, so I am lost what can it be.

N0ctrn
  • 45
  • 5
  • 1
    UIKit has a completely different lifecycle to SwiftUI views (which are structs and therefore recreated each time their content changes). You don't need `@StateObject` to keep the object around. You can just create the object in `viewDidLoad` and store the reference in a property – Paulw11 Apr 23 '22 at 00:06
  • So just var conductor = TunerConductor()? Because when I try to do it this way it says: throwing -10878 a bunch of times, without any more comments, when I run it – N0ctrn Apr 23 '22 at 00:46
  • Please look at the updated post – N0ctrn Apr 23 '22 at 00:57
  • What is the context of the code you have shown? Is it in a view controller subclass? Some other class? The error you are getting is `kAudioUnitErr_InvalidParameter` – Paulw11 Apr 23 '22 at 09:38
  • I have a VC. I copied over the TunerConductor() inside of the VC class, so that the app at least boosts. In my viewDidLoad, I do the same calls which are inside of init(). This made the app run, however, the error still gets printed. – N0ctrn Apr 25 '22 at 23:47

0 Answers0