你好
我想问一下在IOS中使用SWIFT2.0语言进行异常管理的问题。
我有以下代码:
alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
// Remove save value in Keychain
let MyKeychainWrapper = KeychainWrapper()
MyKeychainWrapper.setValue("Account", forKey: kSecAttrAccount as String)
MyKeychainWrapper.setValue("password", forKey: kSecValueData as String)
let loginViewController = self.storyboard?.instantiateViewControllerWithIdentifier("loginViewController") as! LoginViewController
self.presentViewController(loginViewController, animated: true, completion: nil)
}))问题是,我在这段代码中得到了以下异常:
2015-10-26 08:04:49.464 RapidSentryMaster[1907:25789] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<KeychainWrapper 0x7fe783922820> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key v_Data.'我认为异常是因为找不到以前保存的任何KeyChain,所以我尝试用与以前一样的方式来管理异常,例如在android或其他语言中使用try..catch。
alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
// Remove save value in Keychain
do{
let MyKeychainWrapper = KeychainWrapper()
try MyKeychainWrapper.setValue("Account", forKey: kSecAttrAccount as String)
try MyKeychainWrapper.setValue("password", forKey: kSecValueData as String)
}catch{
}
let loginViewController = self.storyboard?.instantiateViewControllerWithIdentifier("loginViewController") as! LoginViewController
self.presentViewController(loginViewController, animated: true, completion: nil)
}))这样做,我遇到了以下问题:
在表达式和catch块中没有对抛出函数的调用是不可访问的.
我想问你,怎样才能正确地处理这样的异常呢?
高级致谢
发布于 2015-10-26 15:02:19
查看KeychainWrapper.h文件。没有方法setValue:forKey:
相反,您应该使用mySetObject:forKey:
如果您想尝试这教程。
干杯。
发布于 2015-10-26 12:55:09
您不能将代码封装在try-catch中,因为在编译时不会向编译器抛出异常。
此外,Swift错误也不是例外,它们的行为就像使用强制处理规则的NSError处理。
https://stackoverflow.com/questions/33345726
复制相似问题