我有一个NSNotification函数,在调用时给我一个错误。我怎样才能解决这个问题?
这是抛出的错误:
无法转换类型'(NSNotification)的值。类型' (又名' NSNotification,Type')是预期的论证类型' NSNotification'
受影响的代码块:
@IBAction func loginBtn(sender: AnyObject) {
let loginobj = Login(userName : self.usernameField.text!, passWord : self.pwdField.text!)
loginobj.getRequest()
handlingAuthentication(NSNotification)
}
这是被称为
的函数func handlingAuthentication(notification: NSNotification) {
let errorDectected = notification.object as! Bool
if(errorDectected){
//initialize Alert Controller
let alertController = UIAlertController(title: "Authentication error", message: AuthHelpers.sharedInstance.errorMessage, preferredStyle: .Alert)
//Initialize Actions
let okAction = UIAlertAction(title: "Ok", style: .Default){
(action) -> Void in
self.dismissViewControllerAnimated(true, completion: nil)
}
//Add Actions
alertController.addAction(okAction)
//Present Alert Controller
self.presentViewController(alertController, animated: true, completion: nil)
}
else
{
print("error not found")
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "isUserLoggedIn")
NSUserDefaults.standardUserDefaults().synchronize()
self.dismissViewControllerAnimated(true, completion:nil)
}
}
初始化NSNotification的代码:
var error = "true"
NSNotificationCenter.defaultCenter().postNotificationName("errorFound", object:error)
我不确定它是否正确,但当我想更改错误的值时,我会这样做error = false
答案 0 :(得分:0)
原来我只需添加
NSNotificationCenter.defaultCenter().postNotificationName("errorPresent", object:errorDict)
在Login.swift中我的函数getRequest()
的底部发布通知和
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.handlingAuthentication(_:)), name:"errorPresent", object: nil)
在我的LoginViewController代码中接收通知以及
func handlingAuthentication(notification: NSNotification) {
let dict = notification.object as! NSDictionary
}
处理传递的数据