SwiftUI: CalculatorButtons with Enumeration and Switches (Ep 2)

  Рет қаралды 23,511

Lets Build That App

Lets Build That App

Күн бұрын

When you have a finite set of data such as calculator buttons, its a good idea to represent them using an Enumeration. I'll go over how to combine this strategy with Switch/Cases to layout the buttons in our application. Enjoy.
Ep 1: • SwiftUI Calculator - M...
Maps UIKit SwiftUI
www.letsbuildthatapp.com/cour...
Fullstack Social NodeJS iOS HTML:
www.letsbuildthatapp.com/cour...
AppStore JSON APIs
www.letsbuildthatapp.com/cour...
Tinder Firestore Swipe and Match
www.letsbuildthatapp.com/cour...
Twitter Slide Out Menu Course
www.letsbuildthatapp.com/cour...
Podcasts Course
www.letsbuildthatapp.com/cour...
Intermediate Training Core Data
www.letsbuildthatapp.com/cour...
Facebook Group
/ 1240636442694543
Completed Source Code
www.letsbuildthatapp.com/cour...
Instagram: / buildthatapp
Twitter: / buildthatapp

Пікірлер: 65
@joeymorque1743
@joeymorque1743 4 жыл бұрын
I’d appreciate another video. I’d like to have a solid understanding using buttons and actions in SwiftUI. Thanks, your videos are very helpful and much appreciated ✌🏼
@dozirlik
@dozirlik 4 жыл бұрын
Really good explanations, very good examples of SwiftUI. I am continuing the next episodes. Thanks.
@ChadZeluff
@ChadZeluff 4 жыл бұрын
Yes I would be interested in seeing this through to completion. Whether that’s the calculation portion, the landscape portion, etc I’d like to see it recreated as much (and as accurately) as possible.
@ahmadmustafa5570
@ahmadmustafa5570 4 жыл бұрын
Great Lesson in SwiftUI Thank you Brian
@curt8806
@curt8806 4 жыл бұрын
I like this way of learning. Thank you.
@placideirandora7405
@placideirandora7405 4 жыл бұрын
Thank you for the content, It's been exciting to follow along. But, I would appreciate it if you continue and complete the project.
@mikedix7969
@mikedix7969 4 жыл бұрын
Yes, would love to get the calculations part!
@smackaroony
@smackaroony 4 жыл бұрын
new upload time I like it
@RajKumar-zn2zk
@RajKumar-zn2zk 4 жыл бұрын
Good one. Thanks Brian :)
@jamilamini7807
@jamilamini7807 4 жыл бұрын
great tutorial please make full course on swiftui advance topics also
@PetersFXfilms
@PetersFXfilms 4 жыл бұрын
11:00 You can simplify the CalculatorButton enum by giving its cases Integer raw values. Then in the switch statement for the title var, check if the rawValue is ≤ 9. If so, convert the raw value to a string and return that.
@alii.haltameemi3967
@alii.haltameemi3967 4 жыл бұрын
well done, it's been a great session, thank you
@marcelcyrus3391
@marcelcyrus3391 3 жыл бұрын
i guess I'm kind of off topic but does anyone know of a good place to watch new movies online ?
@briarjames4038
@briarjames4038 3 жыл бұрын
@Marcel Cyrus I use FlixZone. Just search on google for it =)
@justusjordy3001
@justusjordy3001 3 жыл бұрын
@Briar James Yea, I have been watching on FlixZone for months myself =)
@marcelcyrus3391
@marcelcyrus3391 3 жыл бұрын
@Briar James thank you, I went there and it seems to work :D Appreciate it!!
@briarjames4038
@briarjames4038 3 жыл бұрын
@Marcel Cyrus glad I could help =)
@bodesami9325
@bodesami9325 4 жыл бұрын
Yeah wanna c the calculation part was waiting for it since ep1
@edwurtle
@edwurtle 4 жыл бұрын
How about combine all the number cases into a single case with an associated value? This will reduce the repetitive code for backgroundColor and title. Example use of button: let button: CalculatorButton = .number(value: 4)
@Music-gn1xe
@Music-gn1xe 4 жыл бұрын
Thank you
@BekaDemuradze
@BekaDemuradze 4 жыл бұрын
Yes please continue, I want to see the calculation part
@davidlangley9287
@davidlangley9287 4 жыл бұрын
Once your enum conforms a RawRepresentable (string in this case) tu can directly assign a custom rawValue as: case one = “1”, two = “2”.....
@ibrahimsalah5609
@ibrahimsalah5609 4 жыл бұрын
Yes, I wanna to say that.
@eyupensardeniz6014
@eyupensardeniz6014 4 жыл бұрын
HEY! SO DO YOU MEAN SOMETHING LIKE THIS ? enum CalculatorButtons : RawRepresentable { typealias RawValue = RawRepresentable case one = "1", two = "2", three = "3", four = "4" } Text(button.rawValue) 10 days ago I started software, it doesn't work can you help me figure out this pls. 😅
@sukhmanpreetsingh1525
@sukhmanpreetsingh1525 3 жыл бұрын
@@eyupensardeniz6014 Has been a year, have you got your answer?
@eyupensardeniz6014
@eyupensardeniz6014 3 жыл бұрын
@@sukhmanpreetsingh1525 nope I am still waiting :)
@CoskunCaner
@CoskunCaner 4 жыл бұрын
thanks for this great tutorial, here is my solution: func buttonWidht(_ button:CalcButton? = nil) -> CGFloat { if let b = button, b == .zero { let standard = (UIScreen.main.bounds.width - 5 * 12) / 4 return 2 * standard + 12 } return (UIScreen.main.bounds.width - 5 * 12) / 4 } func buttonHeight() -> CGFloat { return (UIScreen.main.bounds.width - 5 * 12) / 4 }
@dijeferson
@dijeferson 4 жыл бұрын
Very nice SwiftUI intro tutorial! I'd only change the calculation for the button size, as using func buttonWidth -> CGFloat can be misleading and you even had to duplicate code in order to make .zero work. An alternative would be return a CGSize(width, height), and set the values in your buttonWidth func. :)
@nizz4m
@nizz4m 4 жыл бұрын
Brian, can you make video related to combine framework in details perhaps. Please.
@baumannmaurice195
@baumannmaurice195 3 жыл бұрын
Nice video. 👍🏻 Can you please do the calculation part ? It’ll be very nice. Thank you
@datdoesthings9652
@datdoesthings9652 4 жыл бұрын
Great content brian thank you for the video! Will you still be doing UIKit courses or just mostly Swift UI from here on out?
@LetsBuildThatApp
@LetsBuildThatApp 4 жыл бұрын
I predict half and half, SwiftUI apps are just much easier to build out right now.
@andrevdw747
@andrevdw747 4 жыл бұрын
Hey Brian, thanks again for the great video again! Waiting for the next one ;-) Just a quick question about buttons. I have managed to create a button with repeat via Timer.scheduledTimer. In this class i have defined a #selector referring to @objc func but when i put this construct in a separate class file, i got an error... argument of '#selector' cannot refer to global function. (placed in the viewcontroller gives no error btw) Any pointer in the right direction would be great! Thanks!
@chudq
@chudq 4 жыл бұрын
It would be nice to complete this serials with action on buttons. Thanks!
@Denvercoder
@Denvercoder 4 жыл бұрын
You should try using VSCode for the tedious operations like duplicating lines and for stuff like your huge case statement.
@joelmwanja
@joelmwanja 4 жыл бұрын
You can add the "plusMinus" button by pressing "Shift + §" on your Mac. The "§" key is via the top left corner near escape.
@joelmwanja
@joelmwanja 4 жыл бұрын
And for the divide button, simply press Option + /
@ramakrishnad615
@ramakrishnad615 4 жыл бұрын
It was a nice tutorial. Can you make a video on the push to new Screen using Default NavigationView and a Custom Navigation View in SWIFTUI?
@bunkie2100
@bunkie2100 3 жыл бұрын
Here's a question: If I wanted to programmatically change the display value of one of the buttons, how would that work? I ask because I am trying to figure out out to build a fixed-size matrix of buttons (not unlike the calculator example) which will contain (display) values that will change in my app.
@NicosKaralis
@NicosKaralis 3 жыл бұрын
Great content, but I have only one problem with it If you are building an iPad or macOS app, UIApplication.main.bounds.width returns the screen size, not the app size When multitasking this can lead to wrong sizes You should use GeometryReader instead
@jaymartinez311
@jaymartinez311 3 жыл бұрын
For the `0` button just duplicate the function and return what he pasted in the height: func buttonHeight(button: CalculatorButton) -> CGFloat { //__________ return (UIScreen.main.bounds.width - 5 * 12) / 4 } And make sure you also keep the width function too: func buttonWidth(button: CalculatorButton) -> CGFloat { //__________ if button == .zero { return (UIScreen.main.bounds.width - 4 * 12) / 4 * 2 } return (UIScreen.main.bounds.width - 5 * 12) / 4 } Then pass them in the frame: .frame(width: buttonWidth(button: button), height: buttonHeight(button: button)) /*.....................................................................................................................................*/ and also instead of doing a title computed property, just do it in the cases directly: enum CalculatorButton: String { //__________ case decimal = "." case zero = "0", one = "1", two = "2", three = "3", four = "4", five = "5", six = "6", seven = "7", eight = "8", nine = "9" case equals = "=", plus = "+", minus = "-", multiply = "X", divide = "÷" case ac = "AC", plusMinus = "\u{00B1}", //
@MrGamalin
@MrGamalin 4 жыл бұрын
I have used Struct ... struct CalButton : Hashable{ var text : String var color : Color }
@muthurajmuthulingam5427
@muthurajmuthulingam5427 4 жыл бұрын
Can we use struct instead of enum and differentiate while preparing!!!
@foggydawning9268
@foggydawning9268 2 жыл бұрын
🙏🙏🙏
@akhu3034
@akhu3034 Жыл бұрын
Did you read the swift books published by apple cover to cover
@elsayedahmed1162
@elsayedahmed1162 4 жыл бұрын
Please can help me to build custom keyboard for deaf “signs keyboard “
@ModMyTech
@ModMyTech 4 жыл бұрын
Couldn’t you name the enum case variable as it’s string representation? So then you wouldn’t have to do that title variable?
@technooblogy
@technooblogy 4 жыл бұрын
Another easier way to define the size of the zero button: var widthBottons: CGFloat { switch self { case .zero: return 156 default: return 70 } } .frame(width: botton.widthBottons, height: 70, alignment: .center) .cornerRadius(50)
@LetsBuildThatApp
@LetsBuildThatApp 4 жыл бұрын
Hard coded values do not work across all devices
@technooblogy
@technooblogy 4 жыл бұрын
Ok. Thanks Brian, I am trying to learn SWIFT, I do not program since turbo pascal in the 90s :D and I do not find good tutorials in Spanish.
@LetsBuildThatApp
@LetsBuildThatApp 4 жыл бұрын
Cool just remember to run apps on all devices before going to production
@jkristia2011
@jkristia2011 4 жыл бұрын
wow - SwiftUI is awesome, but very slow on my mac mini i5 2014. I was planning on upgrading to the latest i5 and 32GB. Hopefully that will help.
@LetsBuildThatApp
@LetsBuildThatApp 4 жыл бұрын
Mac book pro 16” you won’t regret
@jkristia2011
@jkristia2011 4 жыл бұрын
@@LetsBuildThatApp If only I had that kind of money to spend on a new computer :)
@LetsBuildThatApp
@LetsBuildThatApp 4 жыл бұрын
Time is money
@stpaul4games927
@stpaul4games927 4 жыл бұрын
If you're going to educate people make sure you are doing that close to perfect: 1. Make use of specifying the rawValue instead of providing title to serve exactly the same purpose 2. move your button frame calculation to the CalculatorButton and make to return the actual frame
@LetsBuildThatApp
@LetsBuildThatApp 4 жыл бұрын
Hmm, there are way more issues than just those 2 problems with the code. Try to spot the other 20+ problems with the way things are structured.
@MattDuarte11
@MattDuarte11 4 жыл бұрын
Why not just an array of objects with the params for color ect. Still good tutorial. Looks like Swift finally changed to not use solely storyboards. Thank god
@LetsBuildThatApp
@LetsBuildThatApp 4 жыл бұрын
If you have a finite data set, you can use enums to eliminate a bunch of unnecessary params.
@sasharudenko5446
@sasharudenko5446 4 жыл бұрын
i need continue :)
@abdusalam3ar
@abdusalam3ar 4 жыл бұрын
You speak like a radio host, sir. Great video though.
@Tenly2009
@Tenly2009 3 жыл бұрын
I don’t understand the reason you would lay out 14 out of 16 buttons with their correct labels. It seems monumentally lazy and a bit irresponsible. It would have taken all of 5-10 seconds to finish it up correctly. Taking shortcuts like this when building out interfaces is a poor habit to get into. It creates a high likelihood that one will “forget” to go back and finish it later - often resulting in apps that ship with errors. If it’s about “just demonstrating the concept” and saving time, one row of buttons would have been sufficient - but by the time you do 14 of them, it makes less than zero sense why you would abandon the last 2. 🤦🏻‍♂️ 🤷🏻‍♂️
Reactive Environment Object Application State (Ep 3 SwiftUI)
11:05
Lets Build That App
Рет қаралды 21 М.
When to Semaphore vs Dispatch Group! Careful Multithreaded Shared Resource
11:11
How Many Balloons Does It Take To Fly?
00:18
MrBeast
Рет қаралды 37 МЛН
Вечный ДВИГАТЕЛЬ!⚙️ #shorts
00:27
Гараж 54
Рет қаралды 14 МЛН
When You Get Ran Over By A Car...
00:15
Jojo Sim
Рет қаралды 24 МЛН
iOS Roadmap to Professional Developer: Skills you MUST have!
19:42
Lets Build That App
Рет қаралды 137 М.
SwiftUI Calculator - Master Stacking (Ep 1)
12:40
Lets Build That App
Рет қаралды 47 М.
Google Data Center 360° Tour
8:29
Google Cloud Tech
Рет қаралды 5 МЛН
SwiftUI: UIImagePickerController with UIViewControllerRepresentable
21:14
Lets Build That App
Рет қаралды 18 М.
iOS 15 Async Await JSON Fetching AsncImage Main Actors
31:28
Lets Build That App
Рет қаралды 14 М.
How Many Balloons Does It Take To Fly?
00:18
MrBeast
Рет қаралды 37 МЛН