- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在从 swift3 到 swift4 的转换过程中,转换器已将 NotificationCenter
更改为以下 View :
NotificationCenter.default.addObserver(self, selector: #selector(myController.myFunction(_:)), name: NSNotification.Name.NSTextView.didChangeSelectionNotification, object: myNSTextView)
因此,由于 .addObserver()
中的 selector
,myFunction 现在前面有 @objc。现在编译器报错,NSNotification.Name
类型没有成员 NSTextView
。这是转换器做的,不是我做的。我很困惑。
如何解决这个问题?
更新。我在这里找到了信息How to migrate NSWorkspace notifications to Swift 4?
所以我必须使用
NotificationCenter.default.addObserver(self, selector: #selector(myController.myFunction(_:)), name: NSTextView.didChangeSelectionNotification, object: myNSTextView)
最佳答案
正如 Gary 的评论中提到的,您需要从调用 Selector
切换过来,而是使用回调。以下示例显示了您需要进行哪些设置才能使其正常工作。
var didBecomeActive: (Notification) -> Void = { notification in
print("app became active")
}
private func setupNotification() {
NotificationCenter.default.addObserver(forName: .UIApplicationDidBecomeActive,
object: nil,
queue: OperationQueue.main,
using: didBecomeActive)
}
首先,我创建了 var didBecomeActive
,并将其设为 (Notification) -> Void
类型,以符合函数的预期。在我的示例中,我在回调中保留了 notification
值,但如果您不使用它,则可以将其替换为 _
并且它会正常工作。
接下来,不调用您使用的函数:
NotificationCenter.default.addObserver(self, selector: #selector(myController.myFunction(_:)), name: NSTextView.didChangeSelectionNotification, object: myNSTextView)
我调用以下内容:
NotificationCenter.default.addObserver(forName: <#T##NSNotification.Name?#>,
object: <#T##Any?#>,
queue: <#T##OperationQueue?#>,
using: <#T##(Notification) -> Void#>)
对于 using
参数,只需提供您设置用于接收回调的变量,在本例中为 didBecomeActive
。
关于ios - 如何在 swift4 中修复 NotificationCenter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44555323/
我已经搜索过了,但问题仍然存在。我找到了 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 年前。
我是一名优秀的程序员,十分优秀!