在这里,我正在使用下面的代码检查设备的屏幕是打开还是关闭。我从this SO post获得了这段代码。
代码:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), nil, displayStatusChanged, "com.apple.springboard.lockcomplete", nil, CFNotificationSuspensionBehavior.deliverImmediately)
//CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), nil, displayStatusChanged, "com.apple.springboard.lockstate", nil, CFNotificationSuspensionBehavior.deliverImmediately)
return true
}
}//AppDelegate class end here
func displayStatusChanged(center:CFNotificationCenter,observer: UnsafeMutableRawPointer?,name:CFString,object: UnsafeRawPointer?,userInfo:CFDictionary) -> Void {
}
但是我得到了这个错误:
无法转换类型为'(CFNotificationCenter, UnsafeMutableRawPointer?,CFString,UnsafeRawPointer?,CFDictionary) ->无效”到预期的参数类型“ CFNotificationCallback!” (又名'ImplicitlyUnwrappedOptional <@convention(c) (可选,可选, 可选,可选, 可选)->()>')
有人知道displayStatusChanged
函数在做什么吗?任何帮助,建议或链接将不胜感激。
谢谢
答案 0 :(得分:1)
center
参数应该是可选的(即缺少?
),name
,您有CFString
,但文档中显示CFNotificationName?
CFDictionary
应该是可选的(您缺少?
),如果您将参数与期望的类型匹配,则应该修复错误。
func displayStatusChanged(center: CFNotificationCenter?, observer: UnsafeMutableRawPointer?, name: CFNotificationName?, object: UnsafeRawPointer?, userInfo: CFDictionary?) -> Void {
}