0% found this document useful (0 votes)
17 views4 pages

Mobile Application Written Assignment 6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Mobile Application Written Assignment 6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Written Assignment Unit-6

CS 4405-01 - AY2025-T3

Mobile Applications

Comparison of Structs and Classes in Swift

Structs and classes are fundamental building blocks in Swift, both enabling developers to define

custom data types. However, they have key differences in behavior and usage.

Feature Struct Class

Value Type (copied when Reference Type (shared when


Memory Type
assigned) assigned)

Inheritance No inheritance Supports inheritance

Requires mutating keyword to Properties can be modified


Mutability
modify properties in methods freely in methods

ARC (Automatic Reference Uses ARC for memory


Not applicable
Counting) management
Example:

struct PersonStruct {

var name: String

var person1 = PersonStruct(name: "John")

var person2 = person1 // Copy of value

person2.name = "Doe"

print(person1.name) // Output: John (unchanged)

class PersonClass {

var name: String

init(name: String) {

self.name = name

Code for To-Do List Filtering App

struct ToDo {
var toDoId: Int
var toDoTitle: String
var toDoStatus: Bool
}

let todos = [
ToDo(toDoId: 1, toDoTitle: "Test A", toDoStatus: true),
ToDo(toDoId: 2, toDoTitle: "Test B", toDoStatus: false),
ToDo(toDoId: 4, toDoTitle: "Test C", toDoStatus: true),
ToDo(toDoId: 6, toDoTitle: "Test D", toDoStatus: true)
]

func filterEvenCompletedTodos(todos: [ToDo]) -> [ToDo] {


return todos.filter { $0.toDoId % 2 == 0 && $0.toDoStatus }
}

let filteredTodos = filterEvenCompletedTodos(todos: todos)


print(filteredTodos)
Code for BMI Calculator App

import SwiftUI
enum BMIStatus: String {
case underweight = "You are underweight"
case normal = "Your weight is normal"
case overweight = "You are overweight"
}

class Person {
var weight: Double
var height: Double

init(weight: Double, height: Double) {


self.weight = weight
self.height = height
}

func calculateBMI() -> BMIStatus {


let bmi = weight / (height * height)
switch bmi {
case ..<18.5: return .underweight
case 18.5...24.9: return .normal
default: return .overweight
}
}
}

struct ContentView: View {


@State private var weight = ""
@State private var height = ""
@State private var bmiResult: BMIStatus?

var body: some View {


VStack {
TextField("Enter weight (kg)", text:
$weight).textFieldStyle(RoundedBorderTextFieldStyle())
TextField("Enter height (m)", text: $height).textFieldStyle(RoundedBorderTextFieldStyle())
Button("Calculate BMI") {
if let w = Double(weight), let h = Double(height) {
let person = Person(weight: w, height: h)
bmiResult = person.calculateBMI()
}
}
if let result = bmiResult {
Text(result.rawValue).foregroundColor(result == .normal ? .green : .red)
}
}

}.padding()

Figure-wise explanations:
Figure 2’s topic: BMI Calculator App Input Screen
Users enter their weight (kg) and height (m), then tap the "Calculate BMI" button.

Figure 3’s topic: BMI Output - Underweight Condition


If the calculated BMI is below 18.5, a red message displays: "You are underweight."

Figure 4’s topic: BMI Output - Normal Weight


If the BMI is between 18.5 and 24.9, a green message displays: "Your weight is normal."

Figure 5’s topic: BMI Output - Overweight Condition


If the BMI is 25 or higher, a red message displays: "You are overweight."

References
1. Apple Inc. (2023). Swift Programming Language Guide. Retrieved from
https://developer.apple.com/documentation/swift
2. Raywenderlich Team. (2023). SwiftUI & UIKit: A Developer’s Guide. Retrieved from
https://www.raywenderlich.com

You might also like