|
I bought the course SwiftUI by examples. As I am pacticing each chapter after finishing the project, I had an idea and want to know if it is feasible. If I change the text font, size or so on, it gets updated on screen. I want to add a button "show code" which prints the code as text. I don't want to repeat the code to execute and to print as it is cumbersome and if I foget keeping both in synch, I might have a problem. Example: Following code shows the image. I want to add a button beneath the image, which when toggled shows the following code. Image("rome") .resizable() .frame(height: 200) Is this something doable? |
|
This is possible but you would need to put the code in seperate Strings. You can't print out the code you have written to execute the app. So in your View you have to declare
and then you can show this String. The |
|
How do I execute it? I don't know anyway to run the code using 'myStringToPrint' Psuedo code looks like following: Execute myStringToPrint Text(myStringToPrint) on screen on button press |
|
Am not totally sure what your objectives are. SwiftUI has a See -> Mirror in SwiftUI Documentation But for immediate gratification, create a new project in XCode and paste in this simple view.
When I run this and click the The reflection string reports this change and is printed to the console. Not very pretty, but your homework is to figure out a good way to display the data you need. My ResultsYou can see the For some reason, it cannot decode the font modifier is
Keep CodingPlease return here and, dare I say it?, please reflect on what you learnt. Please share your reflection code. Let us know how you solved your question. |
|
Addendum: I changed the
The reflection added more descriptions to reflect the changes. The output is difficult to read, but that could be solved with some clever parsing. (Your job!) You can find
Good luck! |
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.
Link copied to your pasteboard.