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
}
}
}
}
Topic:
Developer Tools & Services
SubTopic:
Xcode