Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift NotificationCenter.Publisher与PassThroughSubject_Swift_Combine - Fatal编程技术网

Swift NotificationCenter.Publisher与PassThroughSubject

Swift NotificationCenter.Publisher与PassThroughSubject,swift,combine,Swift,Combine,我想通过多个侦听器/订阅者发送一个对象,所以我在查看Combine时看到了两种不同的发布者,即NotificationCenter.Publisher和PassThroughSubject。我不明白为什么有人会在传递主题上使用通知中心.Publisher 我提出了下面的代码,演示了两种方法。总结如下: NotificationCenter.Publisher需要有一个Notification.Name静态属性 这不是真正的类型安全吗(因为我可以为相同的通知发布不同类型的对象。Name/为相同的

我想通过多个侦听器/订阅者发送一个对象,所以我在查看Combine时看到了两种不同的发布者,即
NotificationCenter.Publisher
PassThroughSubject
。我不明白为什么有人会在
传递主题上使用
通知中心.Publisher

我提出了下面的代码,演示了两种方法。总结如下:

  • NotificationCenter.Publisher
    需要有一个
    Notification.Name
    静态属性
  • 这不是真正的类型安全吗(因为我可以为相同的
    通知发布不同类型的对象。Name
    /为相同的
    通知发布不同的发布者。Name
  • 需要在NotificationCenter上发布新值。默认值(不是发布者本身)
  • 显式向下转换到
    map
    闭包中使用的类型
在什么情况下,有人会使用
NotificationCenter.Publisher
over
PassThroughSubject

import UIKit
import Combine

let passThroughSubjectPublisher = PassthroughSubject<String, Never>()
let notificationCenterPublisher = NotificationCenter.default.publisher(for: .name).map { $0.object as! String }

extension Notification.Name {
    static let name = Notification.Name(rawValue: "someName")
}


class PassThroughSubjectPublisherSubscriber {
    init() {
        passThroughSubjectPublisher.sink { (_) in
            // Process
        }
    }
}

class NotificationCenterPublisherSubscriber {
    init() {
        notificationCenterPublisher.sink { (_) in
            // Process
        }
    }
}

class PassThroughSubjectPublisherSinker {
    init() {
        passThroughSubjectPublisher.send("Henlo!")
    }
}

class NotificationCenterPublisherSinker {
    init() {
        NotificationCenter.default.post(name: .name, object: "Henlo!")
    }
}
导入UIKit
进口联合收割机
让passThroughSubjectPublisher=PassthroughSubject()
让notificationCenterPublisher=NotificationCenter.default.publisher(用于:.name).map{$0.object as!String}
扩展通知。名称{
静态let name=Notification.name(rawValue:“someName”)
}
类传递SubjectPublisherSubscriber{
init(){
passThroughSubjectPublisher.sink{(_)in
//过程
}
}
}
类通知中心发布者订阅者{
init(){
notificationCenterPublisher.sink{(_)in
//过程
}
}
}
类传递SubjectPublisherLink{
init(){
passThroughSubjectPublisher.send(“Henlo!”)
}
}
类通知中心发布链接器{
init(){
NotificationCenter.default.post(名称:。名称,对象:“Henlo!”)
}
}

如果您必须使用使用NotificationCenter的第三方框架。

NotificationCenter
可以被视为第一代消息传递系统,而
Combine
是第二代。它有运行时开销,需要强制转换可存储在
Notification
s中的对象。就我个人而言,在构建iOS 13框架时,我永远不会使用
NotificationCenter
,但您确实需要使用它来访问许多仅发布在那里的iOS通知。基本上,在我的个人项目中,除非绝对必要,否则我会将其视为只读

他们将使用NotificationCenter与旧的iOS版本兼容。这是关于NotificationCenter.Publisher的,它仅在iOS 13.0或更高版本上可用,与任何其他发布服务器完全相同