xcode iOS UIDevice Swift

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27496266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:22:20  来源:igfitidea点击:

iOS UIDevice Swift

iosxcodeswift

提问by BCMET25

I'm working on a project and I want to see whether or not the proximity detector is working and what the batteryState is. Here is my code-

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

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

My problem is I just seem to get (Enum value) as my output for BatteryState and the ProximityState is always false (even when held up and screen is black). Also, how can I compare the BatteryState (it is not a string? This is probably noobish but I'm just learning Swift...

我的问题是我似乎只是将(枚举值)作为 BatteryState 的输出,而 ProximityState 始终为 false(即使举起并且屏幕为黑色)。另外,我如何比较 BatteryState(它不是字符串?这可能是小菜一碟,但我只是在学习 Swift ......

回答by Leo Dabus

You should name your functions starting with a lowercase letter. You should do as follow:

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

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.

仅当您的应用程序需要收到接近状态更改的通知时才启用接近监视。否则,禁用接近监控。默认值为假。

并非所有 iOS 设备都有接近传感器。要确定接近度监控是否可用,请尝试启用它。如果proximityMonitoringEnabled 属性的值仍然为false,则邻近监视不可用。

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

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

usage:

用法:

let myBatteryStateDescription = batteryState

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

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