-1

I wanna get object of AppDelegate. This program can build but it will stop running with lldb error.

Maybe the problem is the dirrerence of Swift2.0 and 3.0. My textbook is for swift2.0 but I am using xcode8.0 and Swift3.0.

Error is here.

let ap = UIApplication.shared.delegate as! AppDelegate

I used this page for fixing.

How do I get a reference to the app delegate in Swift?

import UIKit

class FirstViewController: UIViewController {

@IBOutlet weak var dataTextField: UITextField!


let ap = UIApplication.shared.delegate as! AppDelegate


override func viewWillAppear(_ animated: Bool) {
    dataTextField.text = String(ap.cmValue)
}


@IBAction func tapInpu() {

    dataTextField.resignFirstResponder()
    if let text = dataTextField.text{

        if let cmValue = Double(text){
        ap.cmValue = cmValue
        }
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}
Community
  • 1
  • 1

1 Answers1

0

If you remove ap initialization and ap.cmValue = cmValue, does it works ?

If it does work check your outlet referencing in your storyboard you may have old non existent references

Damien
  • 3,322
  • 3
  • 19
  • 29