我一直在尝试使用Swift在Xcode 9中创建一个应用程序,而我正试图在电池电量变化时刷新标签显示。
但每次我使用“label.text = value”时都会收到错误消息,指出实例成员'LineTwo'不能用于'ViewController'类型。
我不确定为什么我会收到此错误。
这是我的完整代码。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var LineTwo: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
UIDevice.current.isBatteryMonitoringEnabled = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
let observer = NotificationCenter.default.addObserver(
forName: NSNotification.Name.UIDeviceBatteryLevelDidChange,
object: nil, queue: nil) {_ in
let msgOne = String(Int(UIDevice.current.batteryLevel*100))+"% ,Battery changed!"
print(msgOne)
LineTwo.text = msgOne //error:Instance member 'LineTwo' cannot be used on type 'ViewController'
}
}