Reputation: 36058
I'm trying to add a breakpoint in the line # gutter, but no breakpoint is added when I do this in the playground. Is this possible or is there another way to set breakpoints in the playground?
Upvotes: 61
Views: 28122
Reputation: 11
You can alternatively use Swift Playgrounds app. It has an in-built feature of Step-Through, and it highlights every line in each step as shown below in the attached image:
Upvotes: 1
Reputation: 162
Instead you can create a swift command line app. Run these lines in command line in order to create a cli app.
mkdir MyCLI
cd MyCLI
swift package init --name MyCLI --type executable
Then run it by:
swift run MyCLI
You can create breakpoints in this app. I copied code samples from documentation.
Upvotes: 0
Reputation: 16715
If you want to pause execution of a playground to have a peek at what's going on, you can use sleep
. The information you can get isn't nearly as granular as what you can get from lldb
.
To do this, you'll need to add import Foundation
at the top of your playground.
Then, wherever you want to pause execution, you can add this:
sleep(10) // 10 second pause...you can make the number whatever you want
Upvotes: 1
Reputation: 1660
I'm just getting my feet wet in Swift, but I think the playground idea is to show the changing state as if you ran in debug and recorded all the variable changes. There's no actual need for a breakpoint as you can see the state at any "point in time". I think it'll take me a while to get used to it, having used a debugger for > 30 years, but should be quite useful for small bits of isolated test code, especially while I'm learning the language.
Upvotes: 0
Reputation: 4539
Matt, I could not enter code in the comments so here is a better view of using a variable on a line by itself to "debug" it.
for index in 1...5 {
dosomething(foo);
foo;
}
Then you can click the eyeball on the right hand side to see a history of foo as it was modified in the loop.
Upvotes: 4