How di

Hi. I have been trying to figure out the code for this program for weeks now and can't figure it out. I apologize for the repeated postings. I have the following code. I am trying to find distance and velocity in the following program. I keep getting error messages.

Any help would be very much appreciated!

Thank you!

 import UIKit
import CoreMotion


let motion = CMMotionManager()


class ViewController: UIViewController {
    
    
    
    
    @IBOutlet var xaxis: UILabel!
    @IBOutlet var yaxis: UILabel!
    @IBOutlet var zaxis: UILabel!
    
    
    let movementManager = CMMotionManager()
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        
        
        movementManager.startAccelerometerUpdates()
        movementManager.accelerometerUpdateInterval = 0.1
            if let data = self.movementManager.accelerometerData {
               self.xaxis.text = String(data.acceleration.x)
                //OLD - self.yaxis.text = String(data.acceleration.y)
                //OLD - self.zaxis.text = String(data.acceleration.z)
                

    
                
                
                var xoutput = self.xaxis.text
                
                var distance: Double = 0.0
                var velocityi: Double = 0.0
                var velocityf: Double = 0.0
                var x: Int = 1
                
                while x == 1 {
                    
                    distance = velocityi * 0.1 + (1/2) * xoutput * pow(0.1, 2)
                    
                    velocityf = velocityi + xoutput * 0.1
                    velocityi = velocityf
                
                    
                    
                }
            }
        
    }
    
}
        
    
    

What error message do you get ?

Where ?

During compilation or execution ?

But you declare xoutput as a String (a text)

var xoutput = self.xaxis.text

and later use it in a computation.

                    distance = velocityi * 0.1 + (1/2) * xoutput * pow(0.1, 2)

You should try;:

var xoutput = Double(self.xaxis.text) ?? 0.0

In addition, you repeat

                while x == 1 { }

As x never change, this will repeat infinetely. Is it what you want ?

Thank you SO MUCH for your help!

I made the modifications below. I am still getting an error message. I also want the while loop to repeat infinitely every 1 second.

Error message: Value of optional type 'String?' must be unwrapped to a value of type 'String'

Coalesce using '??' to provide a default when the optional value contains 'nil'

Force-unwrap using '!' to abort execution if the optional value contains 'nil'

This is in regards to the line:

var xoutput = Double(self.xaxis.text) ?? 0.0


//
//  ViewController.swift
//  Accelerometer
//
//  Created by Brian Cleaver on 8/22/25.
//

import UIKit
import CoreMotion


let motion = CMMotionManager()


class ViewController: UIViewController {
    
    
    
    
    @IBOutlet var xaxis: UILabel!
    @IBOutlet var yaxis: UILabel!
    @IBOutlet var zaxis: UILabel!
    
    
    let movementManager = CMMotionManager()
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        
        
        movementManager.startAccelerometerUpdates()
        movementManager.accelerometerUpdateInterval = 0.1
            if let data = self.movementManager.accelerometerData {
               self.xaxis.text = String(data.acceleration.x)
                //OLD - self.yaxis.text = String(data.acceleration.y)
                //OLD - self.zaxis.text = String(data.acceleration.z)
                

    
                
                
                var xoutput = Double(self.xaxis.text) ?? 0.0

                var distance: Double = 0.0
                var velocityi: Double = 0.0
                var velocityf: Double = 0.0
                var x: Int = 1
                
                while x == 1 {
                    
                    distance = velocityi * 0.1 + (1/2) * xoutput * pow(0.1, 2)
                    
                    velocityf = velocityi + xoutput * 0.1
                    velocityi = velocityf
                
                    
                    
                }
            }
        
    }
    
}

You will need to change the line
var xoutput = Double(self.xaxis.text) ?? 0.0
to
var xoutput = Double(self.xaxis.text ?? "0") ?? 0.0
The self.xaxis.text field is optional

The reason here:

self.xaxis.text is an optional (may be nil).

So you have to address it with the nil coalescing operator

var xoutput = self.xaxis.text ?? "0"

Which is equivalent to:

var xoutput : String
if self.xaxis.text != nil {
  xoutput = self.xaxis.text!
} else {
  xoutput = "0"
}

or a bit more compact:

var xoutput = "0"
if self.xaxis.text != nil {
  xoutput = self.xaxis.text!
} 

For more on this: https://www.hackingwithswift.com/example-code/language/what-is-the-nil-coalescing-operator Note that in the example

print("Hello, \(name ?? "Anonymous")!")

the final ! is not for unwrapping, it is just an exclamation point to be printed at the end of text.

If you were sure it cannot be nil, you could also unwrap directly, but that is really risky.

var xoutput = self.xaxis.text!

Once you are sure that string is a string, not an optional, you can pass it to Double() to convert String to Double.

But here again, if the content is not a number (eg: "abc", Double will return nil. And in all cases it returns an optional.

So here again, use the nil coalescing operator to unwrap (transform optional to a real value) safely.

How di
 
 
Q