为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

四季花海 2019-09-03 16:15:14
我知道Cocoa中有一个UncaughtExceptionHandler,但我正在为Swift寻找同样的东西。即每当应用程序中由于任何错误而未在本地捕获任何错误/异常时,它应该一直冒泡到顶级应用程序对象,在那里我应该能够优雅地处理它并适当地响应用户。Android拥有它。Flex有它。Java有它。想知道Swift为什么缺少这个关键功能。
查看完整描述

3 回答

?
梦里花落0921

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

}


查看完整回答
反对 回复 2019-09-03
?
忽然笑

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

    }

}

}


查看完整回答
反对 回复 2019-09-03
  • 3 回答
  • 0 关注
  • 878 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信