Swift中未捕获的错误/异常处理

我知道Cocoa中有一个UncaughtExceptionHandler,但我正在为Swift寻找同样的东西。即每当应用程序中由于任何错误而未在本地捕获任何错误/异常时,它应该一直冒泡到顶级应用程序对象,在那里我应该能够优雅地处理它并适当地响应用户。


Android拥有它。Flex有它。Java有它。想知道Swift为什么缺少这个关键功能。


四季花海
浏览 879回答 3
3回答

梦里花落0921

这是我用来记录所有异常/错误的代码。Log.error(with:)是一个自定义函数,我存储堆栈跟踪以及其他信息。Thread.callStackSymbols是一个字符串数组,表示堆栈跟踪。func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]? = nil) -> Bool {    NSSetUncaughtExceptionHandler { exception in        Log.error(with: Thread.callStackSymbols)    }    signal(SIGABRT) { _ in        Log.error(with: Thread.callStackSymbols)    }    signal(SIGILL) { _ in        Log.error(with: Thread.callStackSymbols)    }    signal(SIGSEGV) { _ in        Log.error(with: Thread.callStackSymbols)    }    signal(SIGFPE) { _ in        Log.error(with: Thread.callStackSymbols)    }    signal(SIGBUS) { _ in        Log.error(with: Thread.callStackSymbols)    }    signal(SIGPIPE) { _ in        Log.error(with: Thread.callStackSymbols)    }    return true}

忽然笑

enter code here导入UIKit导入CoreDataclass ViewController:UIViewController {@IBOutlet weak var lgnusername: UITextField!@IBOutlet weak var lgnpassword: UITextField!var result = NSArray()override func viewDidLoad() {&nbsp; &nbsp; super.viewDidLoad()&nbsp; &nbsp; // Do any additional setup after loading the view, typically from a nib.}@IBAction func loginaction(_ sender: Any){&nbsp; &nbsp; let app = UIApplication.shared.delegate as! AppDelegate&nbsp; &nbsp; let context = app.persistentContainer.viewContext&nbsp; &nbsp; let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Login")&nbsp; &nbsp; let searchString = self.lgnusername.text&nbsp; &nbsp; let searcghstring2 = self.lgnpassword.text&nbsp; &nbsp; request.predicate = NSPredicate (format: "username == %@", searchString!)&nbsp; &nbsp; do&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; let result = try context.fetch(request)&nbsp; &nbsp; &nbsp; &nbsp; if result.count > 0&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let&nbsp; &nbsp;n = (result[0] as AnyObject).value(forKey: "username") as! String&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let p = (result[0] as AnyObject).value(forKey: "userpassword") as! String&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print("p,n",n,p)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (searchString == n && searcghstring2 == p)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let UserDetailsVc = self.storyboard?.instantiateViewController(withIdentifier: "userViewController") as! userViewController&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //UserDetailsVc.myStringValue = lgnusername.text&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.navigationController?.pushViewController(UserDetailsVc, animated: true)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (searchString == n || searcghstring2 == p)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // print("password incorrect ")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let alertController1 = UIAlertController (title: "no user found ", message: "password incorrect ", preferredStyle: UIAlertControllerStyle.alert)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alertController1.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; present(alertController1, animated: true, completion: nil)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let alertController1 = UIAlertController (title: "no user found ", message: "invalid username ", preferredStyle: UIAlertControllerStyle.alert)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alertController1.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; present(alertController1, animated: true, completion: nil)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("no user found")&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; catch&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; print("error")&nbsp; &nbsp; }}}
打开App,查看更多内容
随时随地看视频慕课网APP