如何将参数传递给 NSNotificationCenter 中的选择器?

How to pass arguments to the selector in NSNotificationCenter?

此代码检查应用何时激活并运行特定方法 dataMain()。我在 dataMain(productCode: String).

中添加了一个参数
NSNotificationCenter.defaultCenter().addObserver(
            self,
            selector: "dataMain",
            name: UIApplicationDidBecomeActiveNotification,
            object: nil)

有什么方法可以将 productCode 参数传递给选择器吗?

如果您在注册此通知时有 productCode 变量,那么您可以使用不同的通知观察方法。

let productCode = "A string"

NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationDidBecomeActiveNotification,
    object: nil,
    queue: NSOperationQueue.mainQueue()) { (notification) -> Void in
        self.dataMain(productCode)
}