TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: SOLVED : a very basic Question: how to select and run a few lines of code only

Forums > 100 Days of SwiftUI

@boat  

So, yesterday I wrote a few lines of code to practic the "func"

Xcode kept giving me error warnings and the func just couldn't print.

However, when I copied the few lines to a new playground file, it worked.

Turned out that the error warnings were for previous codes up somewhere that I could see in one screen.

My question is:

Can I just select the specific lines to run in the debug area ? I don't want that every time I click on the play arrow of the last line , Xcode is running everything I wrote a few weeks back up to date.

Thanks in advance for helping out.

Boat

2      

In Playground, Xcode will try to compile and run all the code that is written there. So without seeing what your initial code was in playground, and what your code was in the new playground file, it is difficult to say exactly what was happening.

If you want to exclude some code from running, without deleting it, there are several thing you could do.

1) Comment out the code using either a) on each line, and before the code to be affected, using the double forward slash //

if a == b {
//   if x == y { …
//   … more code here …
//   }
…  // some active code will be on this line, before the //
}

b) use the comment pairing /* */ at the start and end of the section to be affected. This can be used used on a single line, or over multiple lines

/* single line */

/* over multiple
lines */

2) Delete the code that is not needed.

3      

A playground will run all code on the page up to the line where you clicked the blue arrow. so if you have code on lines 1 through 100 and click the arrow on line 38, only the first 38 lines will be run. It will only be the code from that particular page, plus any code in the shared Sources folder that runs.

Xcode will build your entire project when you run it, so it will pull in code from every file and module.

So if you have a small snippet of code that you want to test out, put it in a playground, either in a new page or above any code you don't want to run, and click the blue arrow.

3      

@boat  

Thank you both @Greenamberred && @roosterboy

I think it is pretty clear for me now :)

Have a great weekend

Boat

2      

@boat  

I just came up with a solution:

When I wanted to test a snippet today, I just went to the top of entire codes, and try the snippet there. I could run the few lines up there without running everything below it.

haha ! could see myself doing this quite often in the future.

:-p

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.