Swift 3 Xcode: 如何将电池电量显示为整数?

Swift 3 Xcode: How to display battery levels as an integer?

我正在制作一个使用 Swift 读取电池百分比的应用程序! 现在我的输出是这样的: 61.0% 或 24.0% 或 89.0% 我要修复的是摆脱 .0,所以它是一个 Int。 到目前为止,这是我的代码:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var infoLabel: UILabel!

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

var timer = Timer()

func scheduledTimerWithTimeInterval(){
    timer = Timer.scheduledTimer(timeInterval: 60, target: self, selector: #selector(self.someFunction), userInfo: nil, repeats: true)
}

func someFunction() {
    self.infoLabel.text = "\(batteryLevel * 100)%"
}

override func viewDidLoad() {
    super.viewDidLoad()
    UIDevice.current.isBatteryMonitoringEnabled = true
    someFunction()
    scheduledTimerWithTimeInterval()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

}

我试过这样的方法:

var realBatteryLevel = Int(batteryLevel)

不过,我明白了 error

我尝试过其他方法,但 none 运气不错。拜托,任何解决方案都会很棒!提前致谢!

编辑

我正在考虑将浮点数 batteryLevel 变成一个字符串,然后将“.0”替换为“”,我在某处看到过这个,但是,我不确定怎么做!

试试这个:

func someFunction() {
    self.infoLabel.text = String(format: "%.0f%%", batteryLevel * 100)
}

为了将来参考,所有 字符串格式说明符 都是 listed here

你只需要在你的函数中转换它:

func someFunction() {
self.infoLabel.text = "\(Int(batteryLevel * 100))%" }

或者,您可以为 batteryLevel 创建一个 Int computed 属性:

var batteryLevel: Int {
  return Int(round(UIDevice.current.batteryLevel * 100))
}

请注意,您可能无法获取电池电量。您应该对此进行测试并显示不同的字符串:

if UIDevice.current.batteryState == .unknown {
  self.batteryLevelLabel.text = "n/a"
} else {
  self.batteryLevelLabel.text = "\(self.batteryLevel)%"
}

另请注意,您应该订阅 .UIDeviceBatteryLevelDidChange 通知,而不是 运行 获取电池电量的计时器。处理所有这些的视图控制器的 "meat" 可能如下所示:

import UIKit

class ViewController: UIViewController {

  @IBOutlet weak var batteryLevelLabel: UILabel!

  ///Holds the notification handler for battery notifications.
  var batteryNotificationHandler: Any?

  ///A computed property that returns the battery level as an int, using rounding.
  var batteryLevel: Int {
    return Int(round(UIDevice.current.batteryLevel * 100))
  }

  ///A function to display the current battery level to a label, 
  ////or the string "n/a" if the battery level can't be determined.
  func showBatteryLevel() {
    if UIDevice.current.batteryState == .unknown {
      self.batteryLevelLabel.text = "n/a"

    } else {
      self.batteryLevelLabel.text = "\(self.batteryLevel)%"
    }
  }

  override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    ///If we have a battery level observer, remove it since we're about to disappear
    if let observer = batteryNotificationHandler {
      NotificationCenter.default.removeObserver(observer: observer)
    }
  }
  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    showBatteryLevel() //display the battery level once as soon as we appear

    //Create a notifiation handler for .UIDeviceBatteryLevelDidChange 
    //notifications that calls showBatteryLevel()
    batteryNotificationHandler =
      NotificationCenter.default.addObserver(forName: .UIDeviceBatteryLevelDidChange,
                                             object: nil,
                                             queue: nil, using: {
                                              (Notification) in
                                              self.showBatteryLevel()
    })
  }

    override func viewDidLoad() {
      super.viewDidLoad()
      //Tell UIDevice that we want battery level notifications
      UIDevice.current.isBatteryMonitoringEnabled = true
  }
}