Swift 3.1 中 NotificationCenter 到 return batteryLevel 和 batteryState 的正确语法是什么

What is the correct syntax for NotificationCenter to return batteryLevel and batteryState in Swift 3.1

我是 Swift 的新手(使用 Xcode 8.3.3,Swift 3.1),我正在尝试显示电池电量和电池状态并在以下时间更新它们价值观改变。这是我目前所拥有的:

import UIKit
class ViewController: UIViewController {

@IBOutlet weak var myBatteryPercent: UILabel!
@IBOutlet weak var myBatteryState: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    UIDevice.current.isBatteryMonitoringEnabled = true


    NotificationCenter.default.addObserver(self, selector: Selector(("batteryLevelDidChange:")),
     name: NSNotification.Name.UIDeviceBatteryLevelDidChange,
     object: nil)

    NotificationCenter.default.addObserver(self, selector: Selector(("batteryStateDidChange:")),
     name: NSNotification.Name.UIDeviceBatteryStateDidChange,
     object: nil)

    var batteryLevel: Float {
        return UIDevice.current.batteryLevel
    }

    var batteryState: UIDeviceBatteryState {
        return UIDevice.current.batteryState
    }

    switch batteryState {
    case .unplugged, .unknown:
        myBatteryState.text = "not charging"
    case .charging, .full:
        myBatteryState.text = "charging or full"
    }

    myBatteryPercent.text = "\(Int((batteryLevel) * 100))%"



    func batteryLevelDidChange (notification: Notification) {
        myBatteryPercent.text = "\(Int((batteryLevel) * 100))%"
    }
    func batteryStateDidChange (notification: Notification) {
        switch batteryState {
        case .unplugged, .unknown:
            myBatteryState.text = "not charging"
        case .charging, .full:
            myBatteryState.text = "charging or full"
        }
    }    
}

我的应用程序在级别或状态更改时崩溃并生成此错误消息:[Battery_Display.ViewController batteryLevelDidChange:]:无法识别的选择器已发送到实例 0x100b0cf10'。

我做错了什么?

基本上您对通知处理程序的放置是错误的。处理程序在 viewDidLoad 方法内声明,它们超出了 NotificationCenter 单例的范围。 只需将它们从 viewDidLoad 中取出即可,它应该可以工作:

import UIKit
class ViewController: UIViewController {

    @IBOutlet weak var myBatteryPercent: UILabel!
    @IBOutlet weak var myBatteryState: UILabel!
    var batteryLevel: Float {
        return UIDevice.current.batteryLevel
    }

    var batteryState: UIDeviceBatteryState {
        return UIDevice.current.batteryState
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        UIDevice.current.isBatteryMonitoringEnabled = true


        NotificationCenter.default.addObserver(self, selector: #selector(batteryLevelDidChange), name: NSNotification.Name.UIDeviceBatteryLevelDidChange, object: nil)

        NotificationCenter.default.addObserver(self, selector: #selector(batteryStateDidChange), name: NSNotification.Name.UIDeviceBatteryStateDidChange, object: nil)

        switch batteryState {
        case .unplugged, .unknown:
            myBatteryState.text = "not charging"
        case .charging, .full:
            myBatteryState.text = "charging or full"
        }

        myBatteryPercent.text = "\(Int((batteryLevel) * 100))%"

    }

    func batteryLevelDidChange (notification: Notification) {
        myBatteryPercent.text = "\(Int((batteryLevel) * 100))%"
    }

    func batteryStateDidChange (notification: Notification) {
        switch batteryState {
        case .unplugged, .unknown:
            myBatteryState.text = "not charging"
        case .charging, .full:
            myBatteryState.text = "charging or full"
        }
    }
}