3 回答
TA贡献1772条经验 获得超5个赞
这是我用来记录所有异常/错误的代码。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
}
TA贡献1806条经验 获得超5个赞
enter code here
导入UIKit导入CoreData
class ViewController:UIViewController {
@IBOutlet weak var lgnusername: UITextField!
@IBOutlet weak var lgnpassword: UITextField!
var result = NSArray()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func loginaction(_ sender: Any)
{
let app = UIApplication.shared.delegate as! AppDelegate
let context = app.persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Login")
let searchString = self.lgnusername.text
let searcghstring2 = self.lgnpassword.text
request.predicate = NSPredicate (format: "username == %@", searchString!)
do
{
let result = try context.fetch(request)
if result.count > 0
{
let n = (result[0] as AnyObject).value(forKey: "username") as! String
let p = (result[0] as AnyObject).value(forKey: "userpassword") as! String
print("p,n",n,p)
if (searchString == n && searcghstring2 == p)
{
let UserDetailsVc = self.storyboard?.instantiateViewController(withIdentifier: "userViewController") as! userViewController
//UserDetailsVc.myStringValue = lgnusername.text
self.navigationController?.pushViewController(UserDetailsVc, animated: true)
}
else if (searchString == n || searcghstring2 == p)
{
// print("password incorrect ")
let alertController1 = UIAlertController (title: "no user found ", message: "password incorrect ", preferredStyle: UIAlertControllerStyle.alert)
alertController1.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(alertController1, animated: true, completion: nil)
}
}
else
{
let alertController1 = UIAlertController (title: "no user found ", message: "invalid username ", preferredStyle: UIAlertControllerStyle.alert)
alertController1.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(alertController1, animated: true, completion: nil)
print("no user found")
}
}
catch
{
print("error")
}
}
}
- 3 回答
- 0 关注
- 878 浏览
添加回答
举报