简体   繁体   English

在 Swift 3.0 中将观察者添加到 NotificationCenter 时遇到问题

[英]Trouble adding observer to NotificationCenter in Swift 3.0

I'm having trouble adding a notification observer in Swift 3.0.我在 Swift 3.0 中添加通知观察者时遇到问题。

Code like so:代码如下:

NotificationCenter.default.addObserver(self, selector: .playerItemDidPlayToEndTime, name: Notification.Name(AVPlayerItemDidPlayToEndTimeNotification), object: playerItem)

I am getting the error: "Cannot invoke value of type Notification.Name.type (aka NSNotification.Name.Type) with argument list (NSNotification.Name)"我收到错误消息: "Cannot invoke value of type Notification.Name.type (aka NSNotification.Name.Type) with argument list (NSNotification.Name)"

With:和:

AVF_EXPORT NSString *const AVPlayerItemDidPlayToEndTimeNotification      NS_AVAILABLE(10_7, 4_0);

What am I doing wrong here?我在这里做错了什么?

As Martin R Commented, name argument should be:正如 Martin R 评论的那样,名称参数应该是:

NSNotification.Name.AVPlayerItemDidPlayToEndTime

And complete code will be:完整的代码将是:

NotificationCenter.default.addObserver(self, selector: .playerItemDidPlayToEndTime, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerItem)

Not in this case but also important: you need to import AVFoundation in your file.不是在这种情况下,但也很重要:您需要在文件中import AVFoundation In Swift the imports are mostly not necessary anymore, so I tend to forget them when needed ...在 Swift 中,大多数情况下都不再需要导入了,所以我倾向于在需要时忘记它们......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM