问题描述:

I have an error with handling exceptions, and every time I open my app after the first launch, the app is terminated. My app is one where, once launched, data is inputted using a UI table view.

Is there a way to catch the error so that the app doesn't terminate? Please help.

Master View Controller:

override func viewDidLoad() {

super.viewDidLoad()

firstLaunchOfApp()

}

//MARK: - Check for first launch

func firstLaunchOfApp() {

let launchedBefore = NSUserDefaults.standardUserDefaults().boolForKey("launchedBefore")

if launchedBefore {

print("Not first launch.")

let index = NSIndexPath(forRow: 0, inSection: 0)

tableView.selectRowAtIndexPath(index, animated: false, scrollPosition: .Top)

pushNewValuesToDetail()

}

else {

print("First launch, setting NSUserDefault.")

NSUserDefaults.standardUserDefaults().setBool(true, forKey: "launchedBefore")

dispatch_async(dispatch_get_main_queue()) {

self.addNewCourseWork()

}

}

}

XCode Message:

Not first launch.

2016-05-11 16:27:45.530 CWCoreData[96559:855380] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:]: row (0) beyond bounds (0) for section (0).'

网友答案:

Your app is crashing because when viewdidload fires the table is not yet populated so their is no row to select.Try doing that in viewdidAppear method.

网友答案:

If you want to catch the exception, Swift's do and catch statements won't help as it's an Objective-C exception.

This answer provides a good solution.

相关阅读:
Top