Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.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
Ios Objective-C observer-如何在Swift中使用_Ios_Objective C_Swift_Swift3 - Fatal编程技术网

Ios Objective-C observer-如何在Swift中使用

Ios Objective-C observer-如何在Swift中使用,ios,objective-c,swift,swift3,Ios,Objective C,Swift,Swift3,我正在为Objective-C应用程序添加Swift的新功能 我在Objective-C(registration.m)中有一位观察员: 在confirm.m中: [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS object:nil]; 如何在Swift中观察这一点?我试过了 NotificationCenter.default.remove

我正在为Objective-C应用程序添加Swift的新功能

我在Objective-C(registration.m)中有一位观察员:

在confirm.m中:

[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS object:nil];
如何在Swift中观察这一点?我试过了

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);
我越来越

未解析标识符的使用 '通知\服务器\已发送\确认\短信'

谢谢

//编辑:

我已在Obj-C中声明:

NSString *const NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS = @"confirmationSMSSent";
这个还能用吗

let name: NSNotification.Name = NSNotification.Name("Your_Notification_Name_Key_String") //NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS
当我使用

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

func confirmationSmsSent(notification: NSNotification) {

}
我出错了

“MyController”类型的值没有成员“ConfirmationMsent”

NotificationCenter.default.addObserver(self,选择器:#选择器(self.confirmationMsent(35;:)),名称:name,对象:nil)


这是因为您尚未声明通知\服务器\发送\确认\短信

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

let NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS = NSNotification.Name("<some string>")
let NOTIFICATION\u SERVER\u SENT\u CONFIRMATION\u SMS=NSNotification.Name(“”)

这是因为您尚未声明通知\u服务器\u发送确认\u短信

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

let NOTIFICATION_SERVER_SENT_CONFIRMATION_SMS = NSNotification.Name("<some string>")
let NOTIFICATION\u SERVER\u SENT\u CONFIRMATION\u SMS=NSNotification.Name(“”)

在Swift 3中,语法已更改。您必须定义
NSNotification.Name的变量

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
}

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

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
}

最有用的swift-lije代码是扩展名

extension Notification.Name {
static let someNewName = "ThatsItImAwsome" 
} 
内贴用法:

.someNewKey

这:


最有用的swift-lije代码是扩展名

extension Notification.Name {
static let someNewName = "ThatsItImAwsome" 
} 
内贴用法:

.someNewKey

这:


在witch文件中,您已声明通知\服务器\发送\确认\ SMS变量?在witch文件中,您已声明通知\服务器\发送\确认\ SMS变量?和self。您的选择器(\:)应定义为func yourSelector()?()中应包含哪些参数?关于func confirmationmssent(notification:NSNotification){}我得到类型为“MyController”的选择器错误值没有成员“confirmationmssent”@sohil-r-memon谢谢!和self.yourSelector(:)应该定义为func yourSelector()?()中应包含哪些参数?关于func confirmationmssent(notification:NSNotification){}我得到类型为“MyController”的选择器错误值没有成员“confirmationmssent”@sohil-r-memon谢谢!