由于SIGABRT而导致应用程序崩溃



我 100% 确定一切都已正确连接,但我不断收到 SIGABRT 错误。

以下是 SelectViewController 的代码:

import UIKit
class SelectViewController: UIViewController {
var accounttype = 0
@IBAction func AdminButton(_ sender: Any) {
}
@IBAction func ParentButton(_ sender: Any) {
}
@IBAction func DriverButton(_ sender: Any) {
}
@IBAction func StudentButton(_ sender: Any) {
accounttype = 1
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var SubmitController = segue.destination as! SubmitViewController
SubmitController.john = accounttype;
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}

以下是 SubmitViewController 的代码:

import UIKit
class SelectViewController: UIViewController {
var accountType = 0
@IBAction func AdminButton(_ sender: Any) {
}
@IBAction func ParentButton(_ sender: Any) {
}
@IBAction func DriverButton(_ sender: Any) {
}
@IBAction func StudentButton(_ sender: Any) {
accountType = 1
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? SubmitViewController {
destination.john = accountType
print("Inside if statement")
}
}

override func viewDidLoad() {
super.viewDidLoad()
print("Out of if statement")
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}

这是控制台:

2018-08-28 17:17:56.529276-0700 SchoolDrive[5178:1005170] 5.4.1 - [Firebase/Analytics][I-ACS023007] Firebase Analytics v.50001000 started
2018-08-28 17:17:56.529921-0700 SchoolDrive[5178:1005170] 5.4.1 - [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (see https://help.apple.com/xcode/mac/8.0/#/dev3ec8a1cb4)
2018-08-28 17:17:57.024884-0700 SchoolDrive[5178:1005198] TIC Read Status [1:0x0]: 1:57
2018-08-28 17:17:57.025067-0700 SchoolDrive[5178:1005198] TIC Read Status [1:0x0]: 1:57
2018-08-28 17:18:01.737912-0700 SchoolDrive[5178:1005175] TIC Read Status [2:0x0]: 1:57
2018-08-28 17:18:01.738363-0700 SchoolDrive[5178:1005175] TIC Read Status [2:0x0]: 1:57
2018-08-28 17:18:03.755460-0700 SchoolDrive[5178:1005196] TIC Read Status [3:0x0]: 1:57
2018-08-28 17:18:03.755664-0700 SchoolDrive[5178:1005196] TIC Read Status [3:0x0]: 1:57
Could not cast value of type 'SchoolDrive.SecondRegisterViewController' (0x10d8349d0) to 'SchoolDrive.SubmitViewController' (0x10d834820).
2018-08-28 17:18:04.613651-0700 SchoolDrive[5178:1004484] Could not cast value of type 'SchoolDrive.SecondRegisterViewController' (0x10d8349d0) to 'SchoolDrive.SubmitViewController' (0x10d834820).
(lldb)

我非常感谢所有的帮助,所有的帮助都是值得赞赏的。提前谢谢你。我需要尽快获得所有帮助。这将使我更容易。再次非常感谢你所有的hep,我非常感谢。

在您的情况下,它正在崩溃,因为强制修复使用if letguard以防止崩溃。将prepare(for:, sender:)方法中的代码修改为下面。

if let destination = segue.destination as? SubmitViewController {
destination.john = accountType
}

guard let destination = segue.destination as? SubmitViewController else {
return 
}
destination.john = accountType

你需要测试 segue.destination 以确保它是一个 SubmitViewController ,然后再用 as!堆栈跟踪表示该实例的类型,如果 SecondRegisterViewController。

根据你的错误说,你可能会将目标segue绑定到错误的VC(SecondRegisterViewController(。预期的是SubmitViewController。 为了更安全,请使用以下方法:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueApplicationToSubmitControllerVC"
{
if let SubmitControllerVC = segue.destination as? SubmitViewController
{
SubmitControllerVC.john = accounttype
}
}
}

最新更新