Xcode Swift 3-如何使用枚举原始值作为NSNotification.Name?

Xcode Swift 3-如何使用枚举原始值作为NSNotification.Name?,xcode,enums,nsnotificationcenter,swift3,Xcode,Enums,Nsnotificationcenter,Swift3,我使用的是Xcode 8 beta 5,我正在尝试设置一个类似这样的通知枚举 enum Notes: String { case note1 case note2 } 然后尝试使用它们作为通知名称 NotificationCenter.default.post(name: Notes.note1.rawValue as NSNotification.Name, object: nil, userInfo: userInfo

我使用的是Xcode 8 beta 5,我正在尝试设置一个类似这样的通知枚举

enum Notes: String {
  case note1
  case note2
}
然后尝试使用它们作为通知名称

NotificationCenter.default.post(name: Notes.note1.rawValue as NSNotification.Name,
                                object: nil, userInfo: userInfo)
但我有个错误

无法将类型“String”的值转换为指定类型“NSNotification.Name”

是否有工作要做,或者我遗漏了什么?它在Xcode 7.3.1中工作


任何帮助都将不胜感激。

在这里,请使用Swift 3&Xcode 8.0

enum Notes: String {

    case note1 = "note1"
    case note2 = "note2"

    var notification : Notification.Name  {
        return Notification.Name(rawValue: self.rawValue )
    }
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        NotificationCenter.default.post(name: Notes.note2.notification ,object: nil, userInfo: nil)
    }
}
另一种方式

import UIKit

extension Notification.Name
{
    enum MyNames
    {
        static let Hello = Notification.Name(rawValue: "HelloThere")
    }
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        NotificationCenter.default.post(name: Notification.Name.MyNames.Hello ,object: nil, userInfo: nil)
    }
}

据我所知,Xcode 7.3.1中捆绑的Swift 2.2.1/SDK中没有类型
NSNotification.Name
,所以我很好奇您是如何做到这一点的

无论如何,如果要使用枚举,您需要编写如下内容:

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

顺便说一下,我建议您使用定义静态属性的扩展名来定义自己的
通知.Name

extension Notification.Name {
    static let note1 = NSNotification.Name("note1")
    static let note2 = NSNotification.Name("note2")
}
(它比enum…长一点,但是)您可以这样使用它:

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

 func newPasscodeSetAction() {
     // Code Here.
 }

我是这样做的,对我来说,这是管理通知名称的更简单的方法

Swift 3.0和Xcode 8.0

使用Notification.Name的扩展名,我们可以在其中定义静态名称,如下所示

extension Notification.Name {
    static let newPasscodeSet = Notification.Name("newPasscodeSet")
    static let userLoggedIn = Notification.Name("userLoggedIn")
    static let notification3 = Notification.Name("notification3")
}
我们可以使用这样的名称:

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

 func newPasscodeSetAction() {
     // Code Here.
 }

希望这个简单的方法对您有所帮助。

也许是swift 4.2中的另一种方法

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

NotificationCenter.default.post(name: Notification.Name.RecordListNotification.recordListTimeDidChange, object: nil)
此外,为了避免冗长:

typealias RecordListNotification = Notification.Name.RecordListNotification
它可以用于:

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

你说得对。Swift 2.2中没有NSNotification.Name。我的意思是我可以用原始值分配通知名称。我应该说得更清楚。@D.Greg,谢谢你的澄清。正如您所知,Swift Beta变化如此之快,我担心我可能遗漏了一些东西。我认为没有理由将“enum MyNames”放在“extension Notification.Name”中。这根本没道理,谢谢你。您还可以创建、构造和定义静态通知名称