Clicky

iOS Dev Nugget 132 XCPCaptureValue in Swift Playgrounds

.

Need to run a code review on your codebase? Hire me

In #131 Running Asynchronous Code in Swift Playgrounds, we wrote about importing XCPlayground and calling XCPSetExecutionShouldContinueIndefinitely() so the asynchronous code can be tested in playgrounds.

We know that when running code such as these in a loop, you can mouse over the line "i" and click the Quick Look button or Show Result button to see the progression of i's value over time.

for i in 1...100 {
    if i % 2 == 0 {
        i
    }
}

Another way to do this is to use the XCPCaptureValue() function in XCPlayground.

for i in 1...100 {
    if i % 2 == 0 {
        XCPCaptureValue("a label", i)
    }
}

The same graph showing the progression of i's value over time will appear in the Assistant editor (Cmd+Opt+Enter). While it's similar to using Quick Look in this simple case, it's much more flexible since you can pass in any value to be logged in the 2nd parameter. The 1st parameter is used as the title of the graph. Values with the same title are logged to the same graph.


Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No

.

.

Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter

.

View archives of past issues