1

Is there any substitute for CFNotificationCenterAddObserver in Swift? In Apple doc's I only find the reference to Objective-C, but I can't find any related code to use with Swift. Any idea?

The code I'm trying to write would be as follows in Objective-C:

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

Edit: It's not a NSNotification(Apple Doc) that I am trying to use as in this question, but an CFNotification(Apple Doc).

Community
  • 1
  • 1
Tchelow
  • 593
  • 4
  • 15
  • @martin-r The question you marked as a duplicated is about a different subject. Can you review it, please? – Tchelow Jun 30 '14 at 05:08
  • Your are right - I have already re-opened the question. – Martin R Jun 30 '14 at 05:11
  • CFNotificationCenterGetDarwinNotifyCenter() and CFNotificationCenterRemoveObserver() are available in Swift, but not CFNotificationCenterAddObserver(). (My guess would be that the "callback function" does not translate into Swift.) You should probably file a bug report at Apple. – Martin R Jun 30 '14 at 05:25
  • @MartinR I don't think it's a bug. I have it from an Apple person that they just expect you to drop into C to pass callback functions. – matt Jun 30 '14 at 05:28
  • @matt: OK, thanks for the information. - (But then it makes not much sense to expose a subset of the CFNotification functions to Swift.) – Martin R Jun 30 '14 at 05:28
  • It really doesn't make much sense. Maybe I'll try to reach someone at Apple before open a bug report. – Tchelow Jun 30 '14 at 05:34
  • Update: Today, Apple announced Swift 2.0 and listed in the feature set was "Closures unified with function pointers". This may solve the issue of not being able to use `CFNotification`'s callbacks in Swift – Blake Merryman Jun 08 '15 at 21:14
  • That's true. But I can't check it now because I'm not working in this project anymore ;) – Tchelow Jun 11 '15 at 06:30

1 Answers1

1

CFNotificationCenterAddObserver is available in Swift now. See apple docs.

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