0

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).'

2 Answers2

1

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.

Community
  • 1
  • 1
Eric
  • 16,003
  • 15
  • 87
  • 139
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.

Ahmad Ishfaq
  • 914
  • 1
  • 8
  • 10