Xcode won't execute code?

Hi everyone. I have the following code that I am trying to execute in Xcode. I then install it on my iPhone. It doesn't run at all and I don't know why. Any thoughts?

Thank you.

import CoreMotion

class MyViewController: UIViewController {
    let motionManager = CMMotionManager()
    
    
    func startAccelerometer() {
        if motionManager.isAccelerometerAvailable {
            motionManager.accelerometerUpdateInterval = 0.1 // 10 updates per second
            motionManager.startAccelerometerUpdates(to: .main) { (data, error) in
                guard let accelerometerData = data else { return }

                let x = accelerometerData.acceleration.x
                let y = accelerometerData.acceleration.y
                let z = accelerometerData.acceleration.z

                // Process the x, y, and z acceleration values here
                print("X: \(x), Y: \(y), Z: \(z)")
                
                
            }
        }
    }

    
}

What do you expect ?

startAccelerometer is never called.

To test, you could add a Button which action will call startAccelerometer().

Thank you for your help! I put this code above the code already mentioned.

inline-code struct ContentView: View { var body: some View { Button("Tap Me") { // Action to perform when the button is tapped print("Button was tapped!") startAccelerometer() } }

        func myFunction() {
            // Your Swift code to be executed
            print("myFunction was called!")
        }
    }
    `inline-code`

I get an error message saying: " Cannot find 'startAccelerometer' in scope"

I wrote the following code above the code in my first post..

   struct ContentView: View {
            var body: some View {
                Button("Tap Me") {
                    // Action to perform when the button is tapped
                    print("Button was tapped!")
                    startAccelerometer()
                }
            }
            }

The error message says "Cannot find 'startAccelerometer' in scope"

Thanks again for the help!

I rewrote the code without errors but the screen is just blank and white.


struct ContentView: View {
    var body: some View {
     
        
    }
}

import CoreMotion

class MyViewController: UIViewController {
    let motionManager = CMMotionManager()
    
    
    func startAccelerometer() {
        if motionManager.isAccelerometerAvailable {
            motionManager.accelerometerUpdateInterval = 0.1 // 10 updates per second
            motionManager.startAccelerometerUpdates(to: .main) { (data, error) in
                guard let accelerometerData = data else { return }
                
                let x = accelerometerData.acceleration.x
                let y = accelerometerData.acceleration.y
                let z = accelerometerData.acceleration.z
                
                // Process the x, y, and z acceleration values here
                print("X: \(x), Y: \(y), Z: \(z)")
                
                
            }
            startAccelerometer()
        }
    }
}






#Preview {
    ContentView()
}

I get the following message in what I think is the debugger window.

Failed to send CA Event for app launch measurements for ca_event_type: 1 event_name: com.apple.app_launch_measurement.ExtendedLaunchMetrics

in this example:

  struct ContentView: View {
            var body: some View {
                Button("Tap Me") {
                    // Action to perform when the button is tapped
                    print("Button was tapped!")
                    startAccelerometer()
                }
            }
            }

you see the error "Cannot find 'startAccelerometer' in scope" because startAccelerometer is outside the scope of the structure ContentView (which begins at its { and ends at its }. The compiler doesn't know you mean the function called startAccelerometer inside your MyViewController class. That's a good thing, because names are often re-used in different places in code.

In your more recent example, your ContentView doesn't refer to startAccelerometer, and there is no code to call it (what happened to your button? Your screen is blank and white because that's what an empty content view looks like. Your startAccelerometer function, if called, would call itself again and again until your program ran out of stack space and was terminated.

You usually don't need a UIViewController in a SwiftUI app. Interfacing between SwiftUI and UIKit is an advanced topic.

I suggest you take a step back and review a few simple SwiftUI projects so you can get the hang of drawing into a window and responding to clicks and drags inside a view. You should also try to learn about basic programming concepts like scope.

Or, if you are just desperate to see what is going on with the accelerometer, here is a quick and dirty program which will print the results into the debugger console in Xcode on your Mac while the program runs on your phone. It is not a good example of app design (most code samples are not)

import SwiftUI
import CoreMotion

struct ContentView: View {
    @State private var motionManager: CMMotionManager = CMMotionManager()
    
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundStyle(.tint)
            Text("Hello, world!")
        }
        .padding()
        .onAppear() {
            
            if motionManager.isAccelerometerAvailable {
                motionManager.accelerometerUpdateInterval = 0.1 // 10 updates per second
                motionManager.startAccelerometerUpdates(to: .main) { (data, error) in
                    guard let accelerometerData = data else { return }
                    
                    let x = accelerometerData.acceleration.x
                    let y = accelerometerData.acceleration.y
                    let z = accelerometerData.acceleration.z
                    
                    // Process the x, y, and z acceleration values here
                    print("X: \(x), Y: \(y), Z: \(z)")
                }
            }
        }
    }
}

Thank you so much! That helped a lot. I have another question if you are able to answer. I just posted it. Thanks again for your help! I really appreciate it!

Xcode won't execute code?
 
 
Q