|
I love the site and find it incredibly useful in trying to learn Swift. I am trying to learn Swift ( I am VERY new to learning Swift) by making a very basic barbell weight plate calculator app. Goal: The user enters a weight value and the app displays the weight plates needed per side of the bar. I wrote a function in playgrounds that does what I expect it to do to tell me how many plates on one side of the bar I would need given a certain goal value. My issue is that I don't understand how to implement this function in SwiftUI in my app so I can enter the value in a text field and get the function to run. Here is my playground code:
Here is attempt to implement it in Swift UI but without any luck. I know it's deconstructed and slightly different - I don't quite understand how to integrate a function into SwiftUI. If someone has any recommendations for resources to look at for this specific ask I would really appreciate it.
As a note, I also posted this question on Stack Overflow here: https://stackoverflow.com/questions/65973652/function-in-swift I really appreciate any guidance and/or help with this! |
|
So, that big if/else in the middle of your
The point being, the logic sits out in the function |
|
I agree with.
also safely unwraps the input amount |
|
Well this being a learning forum, and you already 2 great answers, here's some additional info that I hope helps: 1- Naming: While it certainly is good to have consistency in the way we write code, it is also important to adopt "best practices" for each language. Accordingly, I'm not sure what Also, you can definitely use the underscore in naming, but the convention with Swift is to use camelCase. As you start reading code, you will notice the overwhelming majority use camelCase over underscores. And your In reference to your playground example, your function 2- Conditional Statements: This is more of a personal choice, but I figured I might as well share. Rather than going with the multiple
This, at least for me, is much clearer. It is clearly stating that these conditions must be met, otherwise we have an error. 3- Functions in SwiftUI: The thing about SwiftUI that I believe will help you answer your question is this: "SwiftUI needs to be able to render views which we describe. a Therefore, if you want a function to run, when the TextField changes, then you need the function to be called at the relevant place and be connected to data that changes along with any state property. You can do this via a computed property, or by following one of the 2 other comments. So if you start with some function, and want to translate that into SwiftUI (or UIKit for that matter), focus on the Goal. Not the function. 4- It's all about the Goal: Whether you use functions or computed properties, or anything else really, is up to you. As long as they lead you towards the goal. In your case, the app needs to select an overall weight, at which point the app will recommend how many plates on each side are needed. Since you are restricting the user to your
This way, you can avoid having to check whether the user made any kind of mistake. Also, you can add the 0 option in your array, for just the bar. If you prefer the TextField option, then you can use Conclusion (TL;DR): SwiftUI renders the views each time a state property changes, but best to consider that the changes only occur where necessary instead of a complete re-render of everything on screen. TextFields take a binding, which changes state, therefore, any part that is connected to this state, will change as well. Understanding your data flow will greatly help you with SwiftUI. Think of the data you want, where you want it, and write things accordingly. Avoid lots of code in your body property, and extract into functions or smaller views, whenever possible. Playgrounds are a great way to experiment and try out the business logic. Adding the UI layer should not change that. Keep the logic separate, and the only thing left is the UI elements which can be simple or complex. |
|
Thank you, @sbeitzel, @NigelGee and @MarcusKay for all the help with and thoughts on this! I greatly appreciate it. I am going to work through all of these examples and find what works best for this project. Again, I really appreciate the help as I learn Swift! |
|
Some great points by First I would of added Second I was orginal going with PS you need to add one more With regard to As with the snake_case and camelCase, you can use either but as But always try to keep as much logic out of the Happy coding |
SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!
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.