1

I get Fatal error: Unexpectedly found nil while unwrapping an Optional value Error.

on ViewController.swift.

I initiate serviceController.swift to get json data.

appDelegate.swift

class AppDelegate: UIResponder, UIApplicationDelegate {
 var mainView=ViewController()
...
..

serviceController.swift

self.appDelegate.mainView.gettingData(result:results)

ViewController.swift

public func gettingData(result:Array<String>){

        print("gettingData")
         print(self.currentTime) //it is nill
        print(self.userId)
        print(self.name)

       self.name.text="ss" . //here error Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

maybe It's because initiating on AppDelegate.swift? then, How can I pass data from servicecontroller to viewController.swift?

pd jung
  • 59
  • 1
  • 7
  • You are just creating an instance of `viewController` out of nowhere; so none of it's `@IBOutlet`s will be set. You need to get the reference to your actual view controller. Using the app delegate in this way is not the right approach. You should pass a completion handler to your service controller function so that it can pass the data back to the view controller – Paulw11 Nov 09 '17 at 08:02

0 Answers0