Swift 3 Xcode - 将电池电量显示为整数

时间:2017-04-05 22:39:09

标签: ios swift xcode string-formatting

我正在制作一款使用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

我尝试了其他方法,但没有任何运气。拜托,任何解决方案都会很棒!提前谢谢!

修改

我正在考虑将float batteryLevel变成一个String,然后替换" .0"用""我已经在某个地方看到了这个,但是,我不知道怎么做!

3 个答案:

答案 0 :(得分:4)

请改为尝试:

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

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

答案 1 :(得分:3)

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

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

答案 2 :(得分:1)

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

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通知,而不是运行计时器来获取电池电量。 "肉"处理所有这些的视图控制器可能如下所示:

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