I am a semi retired electrical engineer and need to keep my mind sharp.
I am 70 years old and I have decided to learn swift programming. I have taken python classes and learned the basics.
I am following Chris lessons and find them clear and to the point. Sometimes I have to watch them a couple of times.
I will keep updating this post to comment on my progress.
Welcome to the 70’s club (I’m 70 too). I’ve been learning iOS development since 2018 so you can do this. Good luck and if you have any questions then don’t hesitate to post them.
Thanks Chirs! it is encouraging to know there are other people like my that in the 70’s want to keep learning!.Best regards
Ed
I am trying to work on a screen with two text fields and one button. The purpose is to calculate the product of the two numbers entered in the text filed.
I conniver the text input to Doubles and then have a function that will multiply the two numbers.
This is my codeimport SwiftUI
struct ContentView: View {
@State var text1: String = “”
@State var text2: String = “”
@ State var w = Double()
func calculate() {
let v = Double(text1); let amp = Double(text2)
let W = v * amp
}
var body: some View {
VStack {
TextField(/*@START_MENU_TOKEN@*/"Placeholder"/*@END_MENU_TOKEN@*/,text:$text1)
.keyboardType(/*@START_MENU_TOKEN@*/.decimalPad/*@END_MENU_TOKEN@*/)
TextField(/*@START_MENU_TOKEN@*/"Placeholder"/*@END_MENU_TOKEN@*/,text:$text2)
.keyboardType(/*@START_MENU_TOKEN@*/.decimalPad/*@END_MENU_TOKEN@*/)
Button("Calculate") {
calculate()
}
}
.padding()
}
}
#Preview {
ContentView()
}
I get an error message that say:
Binary operator ‘*’ cannot be applied to two ‘Double?’ operands
What am I doing wrong?
Thanks for your help
TextField values are classified as Optional so what you have to do is test for the case that there is nothing entered in either text1 or text2 prior to doing the calculation. This is known as unwrapping. It’s a bit like opening a box. There might be something inside but there might not be.
You could do it this way where you need to use a guard statement to determine if either v or amp have a value and in either case exit the function. If they both have a value then you can force unwrap each in the calculation :
func calculate() {
let v = Double(text1)
let amp = Double(text2)
// Note that v and amp are now Optionals
// since they may or may not have a value
guard v != nil else { return }
guard amp != nil else { return }
// You can safely force unwrap the values of v and amp
let W = v! * amp!
}
or you could do it this way where you use an if let statement to assign the values and if the values are successfully assigned to v and amp then you can perform the calculation.
func calculate1() {
// This is called unwrapping the values. It's a bit like
// opening a box. There might be something inside but
// there might not be.
if let v = Double(text1), let amp = Double(text2) {
let W = v * amp
}
}
Hope that helps.
Chris Thanks for your help. I had googled for answers but got a lot of different answers that did not make sense.
Now I can continue with my study.
The next thing I need to Lear is how to access a data base and get information from it.
I will go in small steps. Thanks again
I myself a retired computer teacher. I am learning from Chris too. Excellent lessons.
Hi, just butting in. I’m an 84 year old retired computer programmer. Started programming in Swift a few years ago and I have one app in the app store (Bill & Task Manager).
Just thought I’d let you know there are more of us old fogies around. I’ll be happy to help if I can.
Pete
Sorry I had not seen your reply. It is encouraging to know I am not alone on this journey. Congratulations for you app. Please stay in touch.
Sorry I had not seen your reply.
I agree Chris lessons are excellent.
Stay in touch
Stumbled onto this thread accidentally. I am 76 and seeings there are others on here in their 70’s made my morning.
Chris
Thanks for your message, i am also encourage to see people on our age group eager to learn new rhings.