Share
Explore

Recipe to make a simple xcode version 15.4 IOS app to provide a F to C temperature degrees converter

Note: for mac in cloud users:
Since Mac In Cloud cannot do the zip file thing, just copy all your code into a text file, save as studentname_studentid.txt and upload to (Or use a friend’s mac to the work, and upload a Zip File)

mac users: You will make a Zip File of your Project: Name the Zip file as studentName_StudentID.zip and up load to


Here's a step-by-step guide to creating a simple iOS app in Xcode 15.4 to convert temperature from Fahrenheit to Celsius:
Step 1: Create a new project
Open Xcode 15.4 and click on "Create a new Xcode project" in the start window.
Choose the "Single View App" template under the "iOS" section.
Click Next.
Step 2: Set up the project
Enter a project name, e.g., "TempConverter".
Choose a location to save the project.
Select the "Swift" language and "Storyboard" user interface.
Click Next, then Create.
Step 3: Design the user interface
Open the "Main.storyboard" file.
Drag and drop two UITextFields and two UILabels onto the view from the Object Library.
Arrange them to resemble a simple converter layout.
Set the labels to "Fahrenheit:" and "Celsius:".
Step 4: Add constraints
Click on the "Add New Constraints" button in the bottom-right corner.
Add constraints to center the views horizontally and vertically.
Set the width and height of the text fields to be equal.

Step 5: Connect outlets and actions

Open the "ViewController.swift" file. Open Assistant Editor View:
Create IBOutlet and IBAction outlets for the text fields and labels.
Connect the outlets by dragging from the widget to the code in the Assistant Editor View.
Create an action for the "Convert" button.
info

Step 5.1: Open the "ViewController.swift" file

In the Xcode project navigator, locate the "ViewController.swift" file.
Click on the file to open it in the editor.
Step 5.2: Create outlets for the text fields and labels
In the "ViewController.swift" file, add the following code:
Swift
import UIKit

class ViewController: UIViewController {
@IBOutlet weak var fahrenheitTextField: UITextField!
@IBOutlet weak var celsiusLabel: UILabel!
@IBOutlet weak var fahrenheitLabel: UILabel!
@IBOutlet weak var celsiusTextField: UITextField!
}
These lines declare four outlets: two for the text fields and two for the labels.
Step 5.3: Connect the outlets to the views
Open the "Main.storyboard" file.
Select the "ViewController" scene.
Click on the "Show Assistant Editor" button (two circles overlapping) or press ⌘+Option+Enter.
This will open the "ViewController.swift" file in the assistant editor.
Control-click (or right-click) on the "Fahrenheit:" label and drag to the fahrenheitLabel outlet in the code.
Repeat this process for the other three views:
"Fahrenheit" text field to fahrenheitTextField outlet.
"Celsius:" label to celsiusLabel outlet.
"Celsius" text field to celsiusTextField outlet.
Step 5.4: Create an action for the "Convert" button
In the "ViewController.swift" file, add the following code:
Swift
@IBAction func convertToFahrenheit(_ sender: UIButton) {
// Conversion logic will go here
}
This declares an action method for the "Convert" button.
Step 5.5: Connect the action to the "Convert" button
Open the "Main.storyboard" file.
Select the "Convert" button.
Control-click (or right-click) on the button and drag to the convertToFahrenheit action in the code.
Release the mouse button to connect the action.
That's it! You've successfully created outlets for the views and connected them to the code, and also created an action for the "Convert" button.
Now, you can implement the conversion logic in the convertToFahrenheit method, as I mentioned earlier:
Swift
@IBAction func convertToFahrenheit(_ sender: UIButton) {
guard let fahrenheit = Double(fahrenheitTextField.text!) else { return }
let celsius = (fahrenheit - 32) * 5/9
celsiusLabel.text = "\(celsius)"
}
This code will be executed when the user clicks the "Convert" button.


Step 6: Implement the conversion logic
In the "ViewController.swift" file, add the following code:
Swift
@IBAction func convertToFahrenheit(_ sender: UIButton) {
guard let fahrenheit = Double(fahrenheitTextField.text!) else { return }
let celsius = (fahrenheit - 32) * 5/9
celsiusLabel.text = "\(celsius)"
}

ok

Here's a detailed series of steps to implement the conversion logic:

Step 6.1: Open the "ViewController.swift" file
In the Xcode project navigator, locate the "ViewController.swift" file.
Click on the file to open it in the editor.
Step 6.2: Add the conversion logic
In the "ViewController.swift" file, find the convertToFahrenheit method.
This method was created in Step 5.4.
Add the following code inside the method:
Swift
guard let fahrenheit = Double(fahrenheitTextField.text!) else { return }
This line converts the text in the Fahrenheit text field to a Double value.
If the conversion fails (e.g., if the user enters a non-numeric value), the method will exit early.
Step 6.3: Calculate the Celsius value
Add the following line to calculate the Celsius value:
Swift
let celsius = (fahrenheit - 32) * 5/9
This line performs the temperature conversion using the formula: Celsius = (Fahrenheit - 32) × 5/9
Step 6.4: Display the Celsius value
Add the following line to display the Celsius value in the Celsius label:
Swift
celsiusLabel.text = "\(celsius)"
This line sets the text of the Celsius label to the calculated Celsius value.
Step 6.5: Review the complete method
The complete convertToFahrenheit method should look like this:
Swift
@IBAction func convertToFahrenheit(_ sender: UIButton) {
guard let fahrenheit = Double(fahrenheitTextField.text!) else { return }
let celsius = (fahrenheit - 32) * 5/9
celsiusLabel.text = "\(celsius)"
}
Step 6.6: Save the file
Save the "ViewController.swift" file by clicking File > Save or pressing ⌘+S.
That's it! You've successfully implemented the conversion logic. Now, when the user enters a value in the Fahrenheit field and clicks the "Convert" button, the app will display the equivalent Celsius value.


Step 7: Run the app
Click the "Play" button or press ⌘+R to build and run the app.
Test the app by entering a value in the Fahrenheit field and clicking "Convert".
That's it! You now have a simple iOS app that converts temperature from Fahrenheit to Celsius.
Here's the complete code:
Swift
import UIKit

class ViewController: UIViewController {
@IBOutlet weak var fahrenheitTextField: UITextField!
@IBOutlet weak var celsiusLabel: UILabel!
@IBOutlet weak var convertButton: UIButton!

@IBAction func convertToFahrenheit(_ sender: UIButton) {
guard let fahrenheit = Double(fahrenheitTextField.text!) else { return }
let celsius = (fahrenheit - 32) * 5/9
celsiusLabel.text = "\(celsius)"
}
}
Note: This is a basic example, and you may want to add error handling and formatting to the app.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.