trying to set up a table view

Hello, I am trying to set up a table view but I am getting an error when I try to assign the ViewController table delegate and data source. Here is my code:

import UIKit
import CoreMotion
import CoreML
import UserNotifications

protocol Numeric {
   var asDouble: Double { get }
   init(_: Double)
 }
 extension Int: Numeric {var asDouble: Double { get {return Double(self)}}}
 extension Float: Numeric {var asDouble: Double { get {return Double(self)}}}
 extension Double: Numeric {var asDouble: Double { get {return Double(self)}}}
 extension CGFloat: Numeric {var asDouble: Double { get {return Double(self)}}}
 extension Array where Element: Numeric {
   var mean : Element { get { return Element(self.reduce(0, {$0.asDouble + $1.asDouble}) / Double(self.count))}}
   var sd : Element { get {
    let sss = self.reduce((0.0, 0.0)){ return ($0.0 + $1.asDouble, $0.1 + ($1.asDouble * $1.asDouble))}
    let n = Double(self.count)
    return Element(sqrt(sss.1/n - (sss.0/n * sss.0/n)))
   }}
 }
extension Array where Element: Hashable {
  func allEqual(to value: Element) -> Bool {
    let set = Set(self)
    return (set.count == 1 && set.first == value)
  }
}

extension ViewController: UITableViewDelegate {
   
}

extension ViewController: UITableViewDataSource {
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
  }
   
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = "Hello World"
    return cell
  }
}

class ViewController: UIViewController {
   
   
  @IBOutlet weak var gyroX: UILabel!
  @IBOutlet weak var gyroY: UILabel!
  @IBOutlet weak var gyroZ: UILabel!
   
  @IBOutlet weak var accelX: UILabel!
  @IBOutlet weak var accelY: UILabel!
  @IBOutlet weak var accelZ: UILabel!
   
  @IBOutlet weak var quaternionX: UILabel!
  @IBOutlet weak var quaternionY: UILabel!
  @IBOutlet weak var quaternionZ: UILabel!
  @IBOutlet weak var quaternionW: UILabel!
   
  @IBOutlet weak var gravityX: UILabel!
  @IBOutlet weak var gravityY: UILabel!
  @IBOutlet weak var gravityZ: UILabel!
   
  @IBOutlet weak var useraccelX: UILabel!
  @IBOutlet weak var useraccelY: UILabel!
  @IBOutlet weak var useraccelZ: UILabel!
   
  @IBOutlet weak var yaw: UILabel!
  @IBOutlet weak var pitch: UILabel!
  @IBOutlet weak var roll: UILabel!
   
  @IBOutlet weak var predicted_activity: UILabel!
   
  @IBAction func cancelToHome(_ segue: UIStoryboardSegue) {
   }
   
  @IBOutlet weak var notif_log: UITableView!

  
   
   
  struct ModelConstants {
    static let predictionWindowSize = 50
    static let sensorsUpdateInterval = 1.0 / 50.0
    static let stateInLength = 400
  }
   
  var pred_action = "Nothing yet"

  let activityClassificationModel = TC_RF()
  var currentIndexInPredictionWindow = 0

  var accelDataX = Array(repeating: Double(0), count: ModelConstants.predictionWindowSize)
  var accelDataY = Array(repeating: Double(0), count: ModelConstants.predictionWindowSize)
  var accelDataZ = Array(repeating: Double(0), count: ModelConstants.predictionWindowSize)

  var gyroDataX = Array(repeating: Double(0), count: ModelConstants.predictionWindowSize)
  var gyroDataY = Array(repeating: Double(0), count: ModelConstants.predictionWindowSize)
  var gyroDataZ = Array(repeating: Double(0), count: ModelConstants.predictionWindowSize)
   
  var quaternionDataX = Array(repeating: Double(0), count: ModelConstants.predictionWindowSize)
  var quaternionDataY = Array(repeating: Double(0), count: ModelConstants.predictionWindowSize)
  var quaternionDataZ = Array(repeating: Double(0), count: ModelConstants.predictionWindowSize)
  var quaternionDataW = Array(repeating: Double(0), count: ModelConstants.predictionWindowSize)
   
  var gravityDataX = Array(repeating: Double(0), count: ModelConstants.predictionWindowSize)
  var gravityDataY = Array(repeating: Double(0), count: ModelConstants.predictionWindowSize)
  var gravityDataZ = Array(repeating: Double(0), count: ModelConstants.predictionWindowSize)
   
  var useraccelDataX = Array(repeating: Double(0), count: ModelConstants.predictionWindowSize)
  var useraccelDataY = Array(repeating: Double(0), count: ModelConstants.predictionWindowSize)
  var useraccelDataZ = Array(repeating: Double(0), count: ModelConstants.predictionWindowSize)
   
  var yawData = Array(repeating: Double(0), count: ModelConstants.predictionWindowSize)
   
  var rollData = Array(repeating: Double(0), count: ModelConstants.predictionWindowSize)
   
  var pitchData = Array(repeating: Double(0), count: ModelConstants.predictionWindowSize)
   
   
  var stateOutput = try! MLMultiArray(shape:[ModelConstants.stateInLength as NSNumber], dataType: MLMultiArrayDataType.double)
   
  let motionManager = CMMotionManager()
   
  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    notif_log.delegate = self
    notif_log.dataSource = self
  
    UNUserNotificationCenter.current().delegate = self
    MyGyro()
    MyAccel()
    MyQuaternion()
    MyGravity()
    MyUserAccel()
    MyYaw()
    MyPitch()
    MyRoll()
    self.scheduleNotifications(content_body: self.pred_action)
  }
   

The issue is at  notif_log.delegate = self where it gets this error: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

I am not sure why there would be a nil value here. Any advice?

Hi,

I think tableview outlet is not connected in storyboard / xib, and notif_log is not optional, so that's why it is crashing.

I'm pretty sure it is connected properly, I have attached a screenshot. But what do you mean by "notif_log is not optional"?

It is suprising to see View as dataSource and delegate. It should be the VC. If defined in IB, no need to redefine in viewDidLoad.

The notif_log tableView may appear connected but its link being corrupted.

So I would:

  • disconnect the tableView to its IBOutlet
  • Reconnect
  • Correct dataSource and delegate by setting to ViewController
  • Clean Build folder.

Note: it is usually a good idea to give a more specific name to VC: Instead of

class ViewController: UIViewController

name for instance

class AttitudeViewController: UIViewController
trying to set up a table view
 
 
Q