0
@IBOutlet var folderPathAC: NSArrayController!
static var appDelegate = NSApplication.shared.delegate as! AppDelegate
static var context = appDelegate.persistentContainer.viewContext
override func viewDidLoad() {
   super.viewDidLoad()
   updateUITable()
   // Do any additional setup after loading the view.
}

func updateUITable(){
        let request = NSFetchRequest<NSFetchRequestResult>(entityName: "FolderPaths")
        //request.predicate = NSPredicate(format: "age = %@", "12")
        request.returnsObjectsAsFaults = false
        do {
            let result = try ViewController.context.fetch(request)
            for data in result as! [NSManagedObject] {
                let name = data.value(forKey: "folderName") as! String
                let path = data.value(forKey: "folderPath") as! String
                let folder = FolderPath(path: path, name: name)
                self.folderPathAC.addObject(folder)
          }
            
        } catch {
            
            print("Failed")
        }
    }

I am using the above code to populate a table view at the time of the app start in a macOS app. However, I am getting the following exception in the AppDelegate class at the runtime.

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

I am new to Mac development.

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

The most common cause of these are:

failure to unwrap an optional — This can be a forced unwrap (!) or an implicit unwrap (accessing an implicitly unwrapped optional that’s nil).

a failed forced cast (as!), either because the value was a nil optional or because the value was of the wrong type.

AyAz
  • 2,027
  • 2
  • 21
  • 28