- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
Swift 3 中的 NotificationCenter 似乎有一些变化,我似乎不太正确。
使用:
Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1)
我有一个单例对象:
class Notifications {
private static let pipeline = Notifications()
...
接收订阅 NotificationsPipelineProtocol
的项目并将其排入队列。 (它们都是纯 swift,这里没有 Objective-C NSObject。)
private func enqueueNotification(_ notification: NotificationsPipelineProtocol) {
...
在其中它将自己添加为 NotificationCenter 的观察者
NotificationCenter.default.addObserver(self,
selector: #selector(Notifications.didReceiveNotificationCompletion(_:)),
name: notification.completionNotificationName,
object: notification)
注意 - notification.completionNotificationName
是一个生成Notification.Name
项的计算变量。
但是当 NotificationsPipelineProtocol
项发送到 NotificationCenter 时:
NotificationCenter.default.post(name: self.completionNotificationName, object: self)
观察者不调用它关联的订阅方法:
@objc private func didReceiveNotificationCompletion(_ notification : Notification) {
...
你知道为什么吗?有没有办法在 NotificationCenter 中查看特定项目订阅了哪些通知?也许单例对象放弃了它的观察?也许 #selector 的格式不正确?
XCode 没有给我任何警告或错误。
提前致谢。
最佳答案
您正在将 NotificationPipelinesProtocol
对象传递给 addObserver
。这意味着您只会收到该对象发布的通知。如果你想接收由任何对象发布的指定名称的通知,那么你应该传递nil
:
NotificationCenter.default.addObserver(self,
selector: #selector(Notifications.didReceiveNotificationCompletion(_:)),
name: notification.completionNotificationName,
object: nil)
关于ios - Swift 3,NotificationCenter 观察者缺少发布的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43431431/
我已经搜索过了,但问题仍然存在。我找到了 this great question但不幸的是它对我不起作用。这是我第一次使用 NotificationCenter,当我想将数据传递给 XLPagerTa
我目前在下面有这段代码来查看这一天是否已经过去,但它似乎没有用。我是否正确编码? NotificationCenter.default.addObserver(self, selector:"cale
我想检测网络变化。例如,从 wifi 切换到 4G 或返回时。但是我的代码没有检测到这些变化。当应用程序启动时,我只得到一些输出。 我有这个代码: import ReachabilitySwift o
我在 AppDelegate 中调用函数 performActionForShortcutItem 时在应用程序中实现了 3D Touch Quick Action,我通过其中的 Notificati
我已经尝试了好几个小时让观察员收到通知帖子,但每次尝试都失败了。我将观察者放入我的 Root View Controller 中,它是一个嵌入式选项卡 Controller ,并试图让其中一个选项卡
前言 本文主要介绍了关于Swift通知中心(NotificationCenter)使用的相关内容,NotificationCenter是Swift中一个调度消息通知的类,采用单例模式设计,实现传值
收到 IAP 购买通知,然后我从我的服务器请求交易。 如果交易正常,下载歌曲并播放。 我用的是 RxSwift,下面的代码可以用,我想改进一下。 NotificationCenter.default.
我在我的 viewController 中添加了一些观察者 -- applicationWillResignActive、applicationDidEnterBackground 以及许多其他观察者
我想接收来自其他应用程序的通知,这些通知会出现在锁定屏幕上 - 类似于 Pebble 和其他智能 watch 接收它们的方式。 很容易通过注册通知 NSNotificationCenter.defau
我目前正在开发一款在一个 ScrollView 中包含多个文本字段和 2 个 TextView 的应用。在键盘外观上,我已经能够更改 ScrollView 的内容插入,以便不隐藏字段。我遇到的问题是我
我有 3 个通知: NotificationCenter.default.post(name:NSNotification.Name("Notification1"), object: nil) No
我有代码: 主视图.swift: override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(true)
我有一些使用 block /尾随闭包语法创建的通知,如下所示: NotificationCenter.default.addObserver(forName: .NSManagedObjectCont
我不确定我是否在这里做错了什么: 我正在 awakeFromNib 中使用此代码段注册工作区通知 [[[NSWorkspace sharedWorkspace] notificationCenter]
我的项目中有这段代码: func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver(
我正在尝试创建让我的 View 在键盘打开后移动。但我遇到了一个非常意想不到的问题。 这是我的代码: override func viewDidLoad() { super.viewDidLo
我在尝试导入NotificationCenter框架的两个 Controller 中遇到模块错误。我已在“链接的框架和库”中添加了框架,但仍然收到错误。 我什至尝试了cmd + shift + k,但
我已将源代码从 Swift 2.2 更新到 Swift 3.0 以用于推送通知。但我无法获取用户信息。它将返回零。谁能帮忙? 在应用程序委托(delegate)中接收推送通知: func applic
在从 swift3 到 swift4 的转换过程中,转换器已将 NotificationCenter 更改为以下 View : NotificationCenter.default.addObser
这个问题在这里已经有了答案: URLSession.shared.dataTaskPublisher not working on IOS 13.3 (3 个答案) 关闭 3 年前。
我是一名优秀的程序员,十分优秀!