Mobile App Dev 2021W: Tutorial 1: Difference between revisions
Line 22: | Line 22: | ||
<syntaxhighlight lang="swift" line> | <syntaxhighlight lang="swift" line> | ||
// | |||
// Converter-1/Shared/ContentView.swift | |||
// code for Tutorial 1, | |||
// Carleton University COMP 1601 2021W | |||
// by Anil Somayaji | |||
// License: GNU GPLv3 | |||
// | |||
// (But really, you shouldn't be using this code, | |||
// it is only meant for teaching) | |||
import SwiftUI | import SwiftUI | ||
Line 33: | Line 42: | ||
.fontWeight(.bold) | .fontWeight(.bold) | ||
.padding() | .padding() | ||
Spacer() | |||
TextField("Enter Farenheit", text: $Fs) | |||
if let F = Double(Fs) { | if let F = Double(Fs) { | ||
let C = (F - 32.0) * (5/9) | let C = (F - 32.0) * (5/9) | ||
Text("\(Fs) is \( | let Cs = String(format: "%.4g", C) | ||
Text("\(Fs) is \(Cs) Celsius") | |||
} else { | } else { | ||
Text("Enter a number please.") | Text("Enter a number please.") | ||
} | } | ||
Spacer() | |||
} | } | ||
} | } |
Revision as of 06:29, 17 January 2021
This tutorial is still being developed.
In this tutorial you will extend and study a simple app for temperature conversion.
Getting Started
First, you should configure your Xcode environment and make sure you can successfully run an automatically generated hello world program. Once that all works, move on to the exercises below.
Download Converter-1
Download converter-1.zip and unpack it in your code directory. Open the project in Xcode. It should look similar to the hello world program you created previously, except this one has a bit more code. When you refresh the preview, it should look like the following:
Add picture of loaded project
Make sure you can run the project in the simulator.
Code
//
// Converter-1/Shared/ContentView.swift
// code for Tutorial 1,
// Carleton University COMP 1601 2021W
// by Anil Somayaji
// License: GNU GPLv3
//
// (But really, you shouldn't be using this code,
// it is only meant for teaching)
import SwiftUI
struct ContentView: View {
@State private var Fs = ""
var body: some View {
VStack{
Text("Temperature Converter")
.font(.title)
.fontWeight(.bold)
.padding()
Spacer()
TextField("Enter Farenheit", text: $Fs)
if let F = Double(Fs) {
let C = (F - 32.0) * (5/9)
let Cs = String(format: "%.4g", C)
Text("\(Fs) is \(Cs) Celsius")
} else {
Text("Enter a number please.")
}
Spacer()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}