NEW: Learn to build the incredible iOS 15 Weather app today! >>

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

Forums > 100 Days of SwiftUI

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

   

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.

1      

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.

1      

Thank you both @Greenamberred && @roosterboy

I think it is pretty clear for me now :)

Have a great weekend

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

   

Hacking with Swift is sponsored by Sentry

SPONSORED With Sentry’s error and performance monitoring for iOS, you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.

Learn More

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

Reply to this topic…

You need to create an account or log in to reply.

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.