The Xcode Debugger: LLDB

To continue your debugging experiments, you are going to add another bug to your application. Add the code below to ViewController.swift. Notice that you will be using an NSMutableArray, the Objective-C counterpart of Swift’s Array, to make the bug a little harder to find.

@IBAction func buttonTapped(_ sender: UIButton) {
    print("Method: \(#function) in file: \(#file) line: \(#line) called.")

    badMethod()
}

func badMethod() {
    let array = NSMutableArray()

    for i in 0..<10 {
        array.insert(i, at: i)
    }

    // Go one step too far emptying the array (notice the range change):
    for _ in 0...10 {
        array.remove(at: 0)
    }
}

Build and run the application to confirm that a tap on the button results in the application crashing with ...

Get iOS Programming: The Big Nerd Ranch Guide, 6th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.