-1

I need to take the response of the alamofire closure and send it to another class. I get the response inside the closure but when I try to send to the other class, I get: "Fatal error: Unexpectedly found nil while unwrapping an Optional value"

I need to get the response from the closure and set it in a IBOutlet from another class.

Also I try to set the closure response in a variable global and I get the same error.

class DrawInformationViewController: UIViewController {
   @IBOutlet weak var nameLabel: UILabel!

   override func viewDidLoad() {
       super.viewDidLoad()

  }
}

class RequestViewController: UIViewController{
    var getName: String?
    func testAlamofire(){
       if let JSON = response.result.value{
       self.jsonArray = JSON as? NSArray
       //this for doesn't matter it is just for an example, to get the idea
       for item in self.jsonArray! as! [NSDictionary]{
          let name = item["name"] as? String
          print(name) //until here everything is ok
          //the problem is when I try to send this value
          DrawInformationViewController().nameLabel.text = name //here is when it get the error: "Fatal error: Unexpectedly found nil while unwrapping an Optional value"
          //Also I try to set the value in a global var
          getName = name //There is not problem
       }
      }
    }
  }
  func setValueFromClosure(){
     DrawInformationViewController().nameLabel.text = getName
     //Here I get the same error: "Fatal error: Unexpectedly found nil while unwrapping an Optional value" 
  } 
}
Alejandro
  • 1
  • 1

1 Answers1

-1

Try initializing getName as follows:

var getName: String = ""

Cheers.