Difference between revisions of "Mobile App Dev 2021W: Tutorial 1"

From Soma-notes
Jump to navigation Jump to search
Line 14: Line 14:


Make sure you can run the project in the simulator.
Make sure you can run the project in the simulator.
==Tasks==
For each task, explain what you did to accomplish the task or explain your attempts at an answer.  Be sure to include references (links) to any resources that you used for each question, as well as the names of any collaborators.  Be sure to use [https://homeostasis.scs.carleton.ca/~soma/tutorial-template.txt this template].
# Change the format of the output Celsius to have two fixed decimal places rather than have a general format with four significant digits.
# Add a line below the Celsius conversion that gives the temperature in Kelvin.
# Make the temperatures bold only (the rest of the text should be as it was).  It is okay if there is extra spacing between the words and numbers.


==Code==
==Code==

Revision as of 02:51, 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:

Converter-1 in Xcode

Make sure you can run the project in the simulator.

Tasks

For each task, explain what you did to accomplish the task or explain your attempts at an answer. Be sure to include references (links) to any resources that you used for each question, as well as the names of any collaborators. Be sure to use this template.

  1. Change the format of the output Celsius to have two fixed decimal places rather than have a general format with four significant digits.
  2. Add a line below the Celsius conversion that gives the temperature in Kelvin.
  3. Make the temperatures bold only (the rest of the text should be as it was). It is okay if there is extra spacing between the words and numbers.


Code

Converter-1.zip

Converter-1/Shared/ContentView.swift

//
//  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()
    }
}