【发布时间】:2020-08-19 19:32:24
【问题描述】:
我是我的 react native 项目的本地模块,我需要定期从 Objective-C 向 Swift 发送一些数据,所以我使用的是 NSNotificationCenter。我在我的 Swift 类中成功接收到数据,在附加到观察者的函数中,并将其存储在一个属性中。
如果我从任何实例方法调用访问此属性,我可以看到该值已更新。
但是,如果我在附加到 Timer 的选择器函数中访问相同的属性,它看起来好像该值尚未更新,我不知道为什么?似乎计时器选择器函数除了属性的初始值外无权访问任何东西 - 我也尝试将属性作为 userInfo 的一部分传递给 Timer,但问题是一样的。
[[NSNotificationCenter defaultCenter] postNotificationName:@"stateDidUpdate" object:nil userInfo:state];
class StateController {
var state: Dictionary<String, Any> = Dictionary()
var timer: Timer = Timer()
func subscribeToNotifications() {
NotificationCenter.default.addObserver(
self, selector: #selector(receivedStateUpdate),
name: NSNotification.Name.init(rawValue: "stateDidUpdate"), object: nil)
}
@objc func receivedStateUpdate(notification: NSNotification) {
if let state = notification.userInfo {
self.state = (state as? Dictionary<String, Any>)!
print("\(self.state)") // I can see that self.state has been updated here
}
}
func runTimer() {
self.timer = Timer(timeInterval: 0.1, target: self, selector: #selector(accessState(timer:)), userInfo: nil, repeats: true)
self.timer.fire()
RunLoop.current.add(self.timer, forMode: RunLoop.Mode.default)
RunLoop.current.run(until: Date(timeIntervalSinceNow: 2))
}
@objc func accessState(timer: Timer) {
print("\(self.state)") // state is an empty Dictionary when accessed here
}
func printState() {
"\(self.state)" // value printed is the updated value received from the notification
}
}
【问题讨论】:
标签: ios objective-c swift react-native