iosswiftswiftuicore-motioncmmotionmanager

How can I pass Binding<CMMotionManager> in SwiftUI preview?


I passed the motionManager of my app from ContentView to SecondView and it works fine, but i don't know how to pass it in the preview of my SecondView, it wants argument type Binding<CMMotionManager>

ContentView

import SwiftUI
import CoreMotion

struct ContentView: View {
    
    @State private var motionManager = CMMotionManager()
    let queue = OperationQueue()
    @State private var roll = Double.zero
    
    var body: some View {
        NavigationView {
            VStack {
                
                Text("Roll is: \(roll)")
                    .font(.largeTitle)
                
                NavigationLink {
                    SecondView(motionManager: $motionManager)
                } label: {
                    Text("Change View")
                }
            }
            .onAppear {
                //Detect device motion
                self.motionManager.startDeviceMotionUpdates(to: self.queue) { (data: CMDeviceMotion?, error: Error?) in
                    guard let data = data else {
                        print("Error: \(error!)")
                        return
                    }
                    let attitude: CMAttitude = data.attitude
                    
                    DispatchQueue.main.async {
                        self.roll = attitude.roll
                    }
                }
            }
        }
    }
}

SecondView

import SwiftUI
import CoreMotion

struct SecondView: View {
    @Binding var motionManager: CMMotionManager
    
    var body: some View {
        Text("Hello, World!")
    }
}

struct SecondView_Previews: PreviewProvider {
    static var previews: some View {
        SecondView(motionManager: Binding<CMMotionManager>) <-- //What should i pass here??
    }
}

Solution

  • You don't need State/Binding for CMMotionManager, because it is a reference-typed and, as you mentioned yourself, will be single object.

    So just use it by-ref in regular properties

    struct ContentView: View {
        
        private var motionManager = CMMotionManager()   // << here !!
    //...
    }
    
    struct SecondView: View {
        let motionManager: CMMotionManager   // << here !!
        
        var body: some View {
            Text("Hello, World!")
        }
    }
    
    struct SecondView_Previews: PreviewProvider {
        static var previews: some View {
            SecondView(motionManager: CMMotionManager())    // << here !!
        }
    }