ios - iOS Swift 中的异常管理问题

标签 ios swift exception keychain

你好

我想向您询问一个使用 Swift 2.0 语言在 IOS 代码中进行异常管理的问题。

我有以下代码:

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)
 }))

这样做我遇到了以下问题:

try 表达式中不会调用抛出函数 catch block 无法访问...

我想问一下,处理这样的异常的正确方法是什么?

提前致谢

最佳答案

查看 KeychainWrapper.h 文件。没有方法 setValue:forKey:
相反,您应该使用 mySetObject:forKey:
如果你想尝试this教程。
干杯。

关于ios - iOS Swift 中的异常管理问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33345726/

相关文章:

ios - 球体上的快速视频,黑屏但可以听到音频

ios - 如何使用 Swift 检查 iPhone 是否处于飞行模式

ios - 如何从场景停靠栏中删除点击手势识别器?

ios - Collection View 的自定义布局不会更新

java - Eclipse 中通知未处理异常的配置是什么?

ios - 如何让我的 iOS 项目构建正确的版本?

ios - 约束或其 anchor 是否引用不同 View 层次结构中的项目?那是违法的

ios - 将 NSArray 存储到 KeychainItemWrapper 中

python - 如果语句为 False,我可以抛出异常吗?

java - 不使用 try/catch 处理异常