首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >目标-C观察者-如何在Swift中使用

目标-C观察者-如何在Swift中使用
EN

Stack Overflow用户
提问于 2017-02-03 08:25:09
回答 3查看 1.2K关注 0票数 1

我添加了Swift的新功能到Objective应用程序。

我在目标C(登记)中有一位观察员:

代码语言:javascript
复制
[[NSNotificationCenter defaultCenter] removeObserver:self name:NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(confirmSms) name:NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS object:nil];

并以确认方式:

代码语言:javascript
复制
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS object:nil];

如何在Swift中观察这一点?我试过了

代码语言:javascript
复制
NotificationCenter.default.removeObserver(self,
                                                  name: NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS,
                                                  object:nil);

NotificationCenter.default.addObserver(self,
                                              selector:#selector(self.confirmationSmsSent),
                                              name: NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS,
                                              object: nil);

我得到了

未解析标识符“NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS”的使用

谢谢

//编辑:

我已在Obj-C中声明:

代码语言:javascript
复制
NSString *const NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS = @"confirmationSMSSent";

这个还能用吗?

代码语言:javascript
复制
let name: NSNotification.Name = NSNotification.Name("Your_Notification_Name_Key_String") //NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS

当我用

代码语言:javascript
复制
NotificationCenter.default.addObserver(self, selector:#selector(self.confirmationSmsSent(_:)), name: name, object: nil)

func confirmationSmsSent(notification: NSNotification) {

}

我搞错了

类型'MyController‘的值没有成员'confirmationSmsSent’

在……上面

selector:#selector(self.confirmationSmsSent(_:)),名称:名称,对象:0)

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-02-03 08:30:10

在Swift 3中,语法发生了变化。您必须定义NSNotification.Name的变量

代码语言:javascript
复制
let name: NSNotification.Name = NSNotification.Name("Your_Notification_Name_Key_String") //NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS

//Add Notification
NotificationCenter.default.addObserver(self, selector:#selector(self.yourSelector(_:)), name: name, object: nil)

//Remove Notification Observer
NotificationCenter.default.removeObserver(self, name: name, object: nil)

//Your Selector
func yourSelector(_ notification: Notification) {
//Code
}
票数 3
EN

Stack Overflow用户

发布于 2017-02-03 08:28:31

这是因为您还没有声明NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS。

通常通知名称只是一个字符串,顺便说一句,您必须将其转换为嵌套的NSNotification.Name类型。

代码语言:javascript
复制
let NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS = NSNotification.Name("<some string>")
票数 1
EN

Stack Overflow用户

发布于 2017-02-03 08:36:32

最有用的迅捷代码是扩展。

代码语言:javascript
复制
extension Notification.Name {
static let someNewName = "ThatsItImAwsome" 
} 

内帖使用情况:

.someNewKey

这是:

代码语言:javascript
复制
NotificationCenter.default.addObserver(self, selector:#selector(self.yourSelector(_:)), name: .someNewKey, object: nil)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42019788

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档