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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38889125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:08:11  来源:igfitidea点击:

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

xcodeenumsnsnotificationcenterswift3

提问by D. Greg

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

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

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'

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

有解决办法,还是我遗漏了什么?它适用于 Xcode 7.3.1

Any help would be appreciated.

任何帮助,将不胜感激。

回答by

Here you go, Use Swift 3 & Xcode 8.0

给你,使用 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)
    }
}

Another way

其它的办法

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)
    }
}

回答by Gaurav Rami

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

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

Swift 3.0 and Xcode 8.0

Swift 3.0 和 Xcode 8.0

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

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

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.

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

回答by OOPer

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

据我所知,NSNotification.NameXcode 7.3.1 中捆绑的 Swift 2.2.1/SDKs 中没有类型,所以我很好奇你是如何使它工作的。

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.Nameis using extension which defines static properties:

顺便说一句,我最好的建议Notification.Name是使用定义静态属性的扩展:

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:

(它比 enum 长一点...,但是)你可以这样使用它:

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

回答by Reimond Hill

Maybe another approach in swift 4.2

也许是 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)