Swift3.0我想获得AppDelegate的对象

时间:2016-10-09 10:45:31

标签: ios swift swift3

我想获得AppDelegate的对象。 这个程序可以构建,但它将停止运行lldb错误。

问题可能是Swift2.0和3.0的问题。 我的教科书适用于swift2.0,但我使用的是xcode8.0和Swift3.0。

错误就在这里。

让ap = UIApplication.shared.delegate为!的AppDelegate

我使用此页面进行修复。

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.
}


}

1 个答案:

答案 0 :(得分:0)

如果您删除import re full_string = input() #python3 # First split at any one of [, ] or " contents=re.split('[|]|"',full_string) # Now, contents[0] = %h %l %u # contents[1] = %t # contents[2] = %r # contents[3] = %>s %b # For a string, directly using the split function splits the string at the passed argument, which is a space here content_h, content_l, content_u = contents[0].split(' ')[0], contents[0].split(' ')[1:-1], contents[0].split(' ')[-1] content_s, content_b = contents[3].split[' '][0], contents[3].split[' '][1] 初始化和ap,它是否有效?

如果确实有效,请检查您的故事板中引用的插座,您可能有旧的不存在的参考