[英]Swift: Default function parameter value via NotificationCenter
I have a function with default parameter value like this: 我有一个具有默认参数值的函数,如下所示:
@objc func myFunc(theFlag: Bool = false) {
}
This function is called via notification center 通过通知中心调用此功能
NotificationCenter.default.addObserver(self, selector: #selector(myFunc), name: MyNotificationName, object: nil)
When MyNotificationName is posted, myFunc is called via notification center. 发布MyNotificationName时,将通过通知中心调用myFunc 。
I assumed that the default value of theFlag is set to "false" and it works as I expected on most devices. 我假设theFlag的默认值设置为“ false”,并且它在大多数设备上都能正常工作。 However, I found that theFlag is set to "true" on 32bit devices.
但是,我发现在32位设备上,将Flag设置为“ true”。
I wonder if this was not a correct way to call a function with default value via notification center. 我想知道这是否不是通过通知中心调用具有默认值的函数的正确方法。 Is there any official manner to do that?
有官方的方式吗?
I'm testing on Swift 4.1, Xcode 9.4.1 我正在Swift 4.1,Xcode 9.4.1上进行测试
The correct way is 正确的方法是
@objc func myFunc(_ notification:Notification) // OR NSNotification also
Don't expect the NotificationCenter.default
to set a value for theFlag: Bool = false
, with 不要期望
NotificationCenter.default
为theFlag: Bool = false
设置一个值theFlag: Bool = false
,其中
NotificationCenter.default.addObserver(self, selector: #selector(myFunc), name: MyNotificationName, object: nil)
OR 要么
NotificationCenter.default.addObserver(self, selector: #selector(myFunc(_:)), name: MyNotificationName, object: nil)
// //
If you want to send a value then insert it inside object
/ userInfo
如果要发送值,则将其插入
object
/ userInfo
NotificationCenter.default.post(name:MyNotificationName, object:<#Here#>, userInfo:<#OrHere#>)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.