我有一个带有 objc 代码的应用程序,今天使用 swift 代码进行了扩展。 如何将数据从 objc 发送到 swift。这是我的代码,但它不起作用:
对象:
[[NSNotificationCenter defaultCenter] postNotificationName:@"todayExtensionData" object:myData];
swift :
...
NotificationCenter.default.addObserver(self, selector: #selector(self.updateData(notification:)), name: NSNotification.Name("todayExtensionData"), object: nil)
...
@objc func updateData(notification: Notification){
print(notification)
}
最佳答案
您的应用程序和今天的扩展程序在手机上作为单独的进程运行。 NSNotificationCenter 仅在单个进程中工作。
要在您的扩展程序和主应用程序之间传递信息,您可以使用 NSUserDefaults 或共享容器中的文件。
注意:如果您在同一个应用程序中使用上述代码,它将完美运行。
关于objective-c - 将通知从 OBJC 发布到 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57796872/