gpt4 book ai didi

iOS UIDevice swift

转载 作者:可可西里 更新时间:2023-11-01 02:26:57 25 4
gpt4 key购买 nike

我正在做一个项目,我想看看接近检测器是否正常工作以及电池状态是什么。这是我的代码-

import Foundation
import UIKit

class DeviceMonitor {

init() {
UIDevice.currentDevice().batteryMonitoringEnabled = true
UIDevice.currentDevice().proximityMonitoringEnabled = true

//Loops for ease of checking
var timer: Bool = true
while (timer == true){
sleep(2)
BatteryState()
ProximityState()
}
}

func BatteryState() {
var batterystate: UIDeviceBatteryState = UIDevice.currentDevice().batteryState
println(batterystate)
}

func ProximityState() {
var proximitystate: Bool = UIDevice.currentDevice().proximityState
println(proximitystate)
}
}

我的问题是我似乎只是得到(枚举值),因为我的 BatteryState 输出和 ProximityState 始终为 false(即使在被阻止且屏幕为黑色时)。另外,我如何比较 BatteryState(它不是字符串?这可能有点笨,但我只是在学习 Swift...

最佳答案

您应该以小写字母开头来命名您的函数。你应该这样做:

var batteryState: String {
if UIDevice.currentDevice().batteryState == UIDeviceBatteryState.Unplugged {
return "Unplugged"
}
if UIDevice.currentDevice().batteryState == UIDeviceBatteryState.Charging {
return "Charging"
}
if UIDevice.currentDevice().batteryState == UIDeviceBatteryState.Full {
return "Full"
}
return "Unknown"
}

var batteryCharging: Bool {
return UIDevice.currentDevice().batteryState == UIDeviceBatteryState.Charging
}

var batteryFull: Bool {
return UIDevice.currentDevice().batteryState == UIDeviceBatteryState.Full
}

var unPlugged: Bool {
return UIDevice.currentDevice().batteryState == UIDeviceBatteryState.Unplugged
}

Enable proximity monitoring only when your application needs to be notified of changes to the proximity state. Otherwise, disable proximity monitoring. The default value is false.

Not all iOS devices have proximity sensors. To determine if proximity monitoring is available, attempt to enable it. If the value of the proximityMonitoringEnabled property remains false, proximity monitoring is not available.

var proximityState: Bool {
UIDevice.currentDevice().proximityMonitoringEnabled = true

return UIDevice.currentDevice().proximityMonitoringEnabled ? UIDevice.currentDevice().proximityState : false
}

用法:

let myBatteryStateDescription = batteryState

let myProximityStateDescription = proximityState ? "True" : "False"

if proximityState {
// do this
} else {
// do that
}

关于iOS UIDevice swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27496266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com