Swift 3 - How to use enum raw value as NSNotification.Name?

13,550

Solution 1

I am doing this way, For me this is more simple way to manage Notification names.

Swift 3.0 and Xcode 8.0

Using extension of Notification.Name, we can define static names inside that as following.

extension Notification.Name {
    static let newPasscodeSet = Notification.Name("newPasscodeSet")
    static let userLoggedIn = Notification.Name("userLoggedIn")
    static let notification3 = Notification.Name("notification3")
}

We can use that names like this:

 override func viewDidLoad() {
      NotificationCenter.default.addObserver(self, selector: #selector(self.newPasscodeSetAction), name: .newPasscodeSet, object: nil)
 }

 func newPasscodeSetAction() {
     // Code Here.
 }

Hope this simple way helpful for you.

Solution 2

As far as I know, there was no type NSNotification.Name in Swift 2.2.1/SDKs bundled in Xcode 7.3.1, so I'm curious how you have made it work.

Anyway you need to write something like this if you want to utilize your enum:

NotificationCenter.default.post(name: NSNotification.Name(Notes.note1.rawValue),
                                object: nil, userInfo: userInfo)

By the way, my best recommendation to define your own Notification.Name is using extension which defines static properties:

extension Notification.Name {
    static let note1 = NSNotification.Name("note1")
    static let note2 = NSNotification.Name("note2")
}

(It's a little bit longer than enum..., but) you can use it like this:

NotificationCenter.default.post(name: .note1,
                                object: nil, userInfo: userInfo)

Solution 3

Maybe another approach in swift 4.2

extension Notification.Name{
   struct RecordListNotification {
        static let recordListDidChange:Notification.Name = Notification.Name("recordListDidChange")
        static let recordListTimeDidChange = Notification.Name("recordListTimeDidChange")
    }
}

and then

NotificationCenter.default.post(name: Notification.Name.RecordListNotification.recordListTimeDidChange, object: nil)

also to avoid verbose:

typealias RecordListNotification = Notification.Name.RecordListNotification

And it can be used:

NotificationCenter.default.post(name: RecordListNotification.recordListTimeDidChange, object: nil)
Share:
13,550
D. Greg
Author by

D. Greg

Updated on June 10, 2022

Comments

  • D. Greg
    D. Greg about 2 years

    I'm using Xcode 8 beta 5 and I'm trying to setup an enum of notifications like this

    enum Notes: String {
      case note1
      case note2
    }
    

    Then trying to use them as the notification names

    NotificationCenter.default.post(name: Notes.note1.rawValue as NSNotification.Name,
                                    object: nil, userInfo: userInfo)
    

    But I'm getting an error.

    Cannot convert value of type 'String' to specified type 'NSNotification.Name'

    Is there a work around, or am I missing something? It works in Xcode 7.3.1

    Any help would be appreciated.

  • D. Greg
    D. Greg almost 8 years
    You're right. There isn't NSNotification.Name in Swift 2.2. I meant that I could just assign the notification name with the raw value. I should have been more clear.
  • OOPer
    OOPer almost 8 years
    @D.Greg, thanks for clarification. As you know Swift betas are so rapidly changing, I was afraid I might be missing something.
  • Reimond Hill
    Reimond Hill over 5 years
    thank you. You can also create and struct and define static Notification names