【问题标题】:CFNotificationCenterAddObserver in SwiftSwift 中的 CFNotificationCenterAddObserver
【发布时间】:2014-06-30 04:51:48
【问题描述】:

Swift 中的CFNotificationCenterAddObserver 有什么替代品吗? 在Apple doc's 中,我只找到了对Objective-C 的引用,但我找不到任何与Swift 一起使用的相关代码。有什么想法吗?

我正在尝试编写的代码在 Objective-C 中如下所示:

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
            NULL,
            displayStatusChanged,
            CFSTR("com.apple.iokit.hid.displayStatus"),
            NULL,
            CFNotificationSuspensionBehaviorDeliverImmediately);

编辑:我尝试使用的不是NSNotification(Apple Doc),而是CFNotification(Apple Doc)。

【问题讨论】:

  • @martin-r 您标记为重复的问题是关于不同主题的。请你复习一下好吗?
  • 你是对的 - 我已经重新打开了这个问题。
  • CFNotificationCenterGetDarwinNotifyCenter() 和 CFNotificationCenterRemoveObserver() 在 Swift 中可用,但 CFNotificationCenterAddObserver() 不可用。 (我的猜测是“回调函数”不会翻译成 Swift。)你应该向 Apple 提交错误报告。
  • @MartinR 我不认为这是一个错误。我从一位 Apple 人员那里得知,他们只是希望您使用 C 语言来传递回调函数。
  • @matt:好的,感谢您提供的信息。 - (但是,将 CFNotification 函数的子集暴露给 Swift 并没有多大意义。)

标签: ios iphone swift


【解决方案1】:

CFNotificationCenterAddObserver 现在可以在 Swift 中使用。见apple docs

func CFNotificationCenterAddObserver(_ center: CFNotificationCenter!,
                               _ observer: UnsafePointer<Void>,
                               _ callBack: CFNotificationCallback,
                               _ name: CFString!,
                               _ object: UnsafePointer<Void>,
                               _ suspensionBehavior: CFNotificationSuspensionBehavior)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 2014-07-24
    • 2014-08-17
    • 2015-01-25
    相关资源
    最近更新 更多