Debugging tool you (probably) are not using

Davit K.
3 min readFeb 20, 2023

--

How many times are you printing something in the console? If the project you are working on takes a long time to compile “Print Debugging” can be quite slow. In this short article I want to show you how can you print without rebuilding your app.

First of all we will create a button which will load users. Nothing special.

Let’s say you want to check if data exist. Normally you would write something like this:

print(String(data: data!, encoding: .utf8))

and it is fine. But after every new print you have to rebuild your project. And if it takes a long time… it’s a pain. But there is a better way. Double tap on the breakpoint right after completionHandler and you should see something like this.

Then, press on the “Add Action” button and choose “Debugger command”. In the textfield just paste this:

po String(data: data!, encoding: .utf8)

If you enable the “Automatically continue after evaluating actions” checkbox Xcode will not stop at this breakpoint.

Run the app, tap on the button and you should see print in the console. The best part is there is no need to recompile the app when adding or editing your breakpoints.

Using User Breakpoint on Textfields

Another way to use custom breakpoints is to fill textfields. Let’s say you working on the app that requires to log in. Normally you’ll write your email and password, then click on the Sing Up button etc… Or your textfields will have a default text, something like this:

emailTextField.text = "test@email.com"
paswordTextField.text = "qwerty1"

With breakpoints we can automatically set values without changing any line of code. How it works. Let’s say we have simple login screen.

class ViewController: UIViewController {

let emailTextField = UITextField()
let passwordTextField = UITextField()
let loginButton = UIButton()

override func viewDidLoad() {
super.viewDidLoad()

view.addSubview(emailTextField)
view.addSubview(passwordTextField)
view.addSubview(loginButton)

emailTextField.frame = .init(x: 12, y: 120, width: view.bounds.width - 24, height: 42)
emailTextField.placeholder = "Email"
emailTextField.borderStyle = .roundedRect
passwordTextField.frame = .init(x: 12, y: 170, width: view.bounds.width - 24, height: 42)
passwordTextField.placeholder = "Password"
passwordTextField.borderStyle = .roundedRect

loginButton.frame = .init(x: 12, y: 230, width: view.bounds.width - 24, height: 42)
loginButton.setTitle("Login", for: .normal)
loginButton.backgroundColor = .systemBlue
loginButton.layer.cornerRadius = 8
}
}

Create custom breakpoint before viewDidLoad() closing bracket, choose “Debugger Command” action and paste this line of code. Also enable “Automatically continue after evaluating actions” checkbox.

expr emailTextField.text = "YOUREMAIL@gmail.com"

and… voila!! The beauty of breakpoints is you can press on your custom breakpoint, change email and

Of course, we want to add text for password textfield. Double tap on breakpoint, tap + and add default text for password textfield.

Run the app. Your text fields are already set without having to change one line of code.

--

--