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

5
  • 1
    probably tableview is not loaded yet, call tableview.reloadData before that,or select in viewWillAppear Commented May 11, 2016 at 16:03
  • tried placing "firstLaunchOfApp()" in ViewWillAppear - same error. How would I go about calling tableview.reloadData? Commented May 11, 2016 at 16:08
  • The table.reloadData won't work as view has not loaded yet.Try the code in viewdidAppear Commented May 11, 2016 at 16:09
  • view has loaded and thats why viewDidLoad is called :) even the name says it. Commented May 12, 2016 at 8:30
  • Does this answer your question? Catching NSException in Swift Commented Mar 9, 2020 at 14:48

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

4 Comments

I said viewdidAppear not ViewWillAppear :)
Don't have ViewDidAppear in my file. Do I just create it?
Yes it is a function just like viewdidload.
override func viewDidAppear(animated: Bool) { }.This is the function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.