2017-04-05 76 views
2

我正在製作應用程序以使用Swift讀取電池電量百分比! 現在我的是這樣的: 61.0%或24.0%或89.0% 我試圖解決的是擺脫.0所以它是一個Int。 這是我到目前爲止的代碼:Swift 3 Xcode - 以整數顯示電池電量

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

我已經嘗試過其他方法,但如果運氣好的話沒有。請,任何解決方案將是真棒!提前致謝!

編輯

我正考慮浮batteryLevel爲一個字符串,然後用‘’代替」 .0" ,我看到了這個地方,但是,我不知道怎麼辦!

+0

在'someFunction'作爲計算的變量 – Paulw11

+1

執行整數轉換或聲明'realBatteryLevel' @ Paulw11不幸的是,無論什麼原因都的這些方法不會導致錯誤,但他們都使我的輸出0% –

回答

4

試試這個:

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

以供將來參考,所有字符串格式說明listed here

+1

謝謝這工作完美。正是我在找的! –

+2

因此,將其標記爲已接受的答案 –

3

你只需要將其轉換自己的函數中:

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

'batteryLevel'是一個標準化值(即介於'0.0'和'1.0'之間)。因此,大多數情況下'Int(batteryLevel)'表達式將* zero *。 –

+0

正如@PauloMattos所說,我在之前的評論中說過,這隻會讓我的輸出每次都等於0%。感謝您嘗試 –

+0

我編輯了我的答案,嘗試'Int(batteryLevel * 100)' –

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

正如有人在一個現在被刪除的評論中指出的那樣,最好爲'.UIDeviceBatteryLevelDidChange '通知**和**'。UIDeviceBatteryStateDidChange'通知,所以您可以知道何時要獲取電池電量通知,並且在嘗試讀取電池狀態之前每次都不需要檢查UIDevice.current.batteryState。 –