post 到通知中心是什么意思
What does post to notification center mean
post通知中心是什么意思?
post 和使用 NotificationCenter 添加观察者有什么区别。
只是通知中心用来post信息给注册的观察者
Information about NotificationCenter
Example
NSNotificationCenter : NSNotificationCenter 可以被视为一个接口,用于在 app.Unlike 推送或本地通知中传达信息,您可以在其中通知用户您想要的任何内容就像他们接收一样,NSNotificationCenter 允许我们根据应用程序中发生的操作在 classes and/or structs 之间发送和接收信息。 NotificationCenter 可以简单地被认为是一个广播公司,我们调到一些 stations/channels 以接收更改(如果有的话)。
NotificationCenter.default是观察所有通知的地方,posted.Each通知有一个唯一的标识符,可以用来在广播端和接收端验证频道。
addObserver() :对象向通知中心注册以使用 addObserver(_:selector:name:object:) 或 addObserver(forName:object:queue:使用 :) 方法。当一个对象将自己添加为观察者时,它指定了它应该接收哪些通知。因此,一个对象可能会多次调用此方法,以便将自己注册为几个不同的观察者 notifications.The class 实施 addobserver() 方法是接收者。
示例:添加一个观察者(这是接收端的)
NotificationCenter.default.addObserver(self, selector: #selector(self.methodOfReceivedNotification(notification:)), name: Notification.Name("NotificationIdentifier"), object: nil)
@objc func methodOfReceivedNotification(notification: Notification){}
post() : 创建具有给定名称和发件人的通知,并将其 post 发送到通知中心。创建一个包并通过通道发送它。实施 post() 方法的 class 是 broadcaster.
示例:发布观察者(这是广播端的意愿)
NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: nil)
Note that the "NotificationIdentifier" is the unique name to identify the particular channel. And selector is method/action that need to be performed when a notification is received.You can also pass the data within the notification center within the "object" parameter.
"With respect to your question "post 和使用 NotificationCenter 添加观察者有什么区别。
答案是他们都是正面交锋,一个(add-observer())用于发送,另一个(post())用于用于接收。因此,如果您要 post 发送通知,则必须实施一个观察者 too.In 简而言之,如果您扔东西,您需要有人接住,如果您说话,则需要有人倾听。
post通知中心是什么意思? post 和使用 NotificationCenter 添加观察者有什么区别。
只是通知中心用来post信息给注册的观察者
Information about NotificationCenter
Example
NSNotificationCenter : NSNotificationCenter 可以被视为一个接口,用于在 app.Unlike 推送或本地通知中传达信息,您可以在其中通知用户您想要的任何内容就像他们接收一样,NSNotificationCenter 允许我们根据应用程序中发生的操作在 classes and/or structs 之间发送和接收信息。 NotificationCenter 可以简单地被认为是一个广播公司,我们调到一些 stations/channels 以接收更改(如果有的话)。
NotificationCenter.default是观察所有通知的地方,posted.Each通知有一个唯一的标识符,可以用来在广播端和接收端验证频道。
addObserver() :对象向通知中心注册以使用 addObserver(_:selector:name:object:) 或 addObserver(forName:object:queue:使用 :) 方法。当一个对象将自己添加为观察者时,它指定了它应该接收哪些通知。因此,一个对象可能会多次调用此方法,以便将自己注册为几个不同的观察者 notifications.The class 实施 addobserver() 方法是接收者。
示例:添加一个观察者(这是接收端的)
NotificationCenter.default.addObserver(self, selector: #selector(self.methodOfReceivedNotification(notification:)), name: Notification.Name("NotificationIdentifier"), object: nil)
@objc func methodOfReceivedNotification(notification: Notification){}
post() : 创建具有给定名称和发件人的通知,并将其 post 发送到通知中心。创建一个包并通过通道发送它。实施 post() 方法的 class 是 broadcaster.
示例:发布观察者(这是广播端的意愿)
NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: nil)
Note that the "NotificationIdentifier" is the unique name to identify the particular channel. And selector is method/action that need to be performed when a notification is received.You can also pass the data within the notification center within the "object" parameter.
"With respect to your question "post 和使用 NotificationCenter 添加观察者有什么区别。
答案是他们都是正面交锋,一个(add-observer())用于发送,另一个(post())用于用于接收。因此,如果您要 post 发送通知,则必须实施一个观察者 too.In 简而言之,如果您扔东西,您需要有人接住,如果您说话,则需要有人倾听。