0% found this document useful (0 votes)
7 views15 pages

Swift Structs vs Classes and Apps Guide

The document compares structs and classes in Swift, highlighting their differences in type, memory allocation, inheritance, and mutability. It also includes examples of a To-Do List app with filtering logic and a BMI Calculator app, demonstrating the use of structs and classes in practical applications. The document emphasizes the importance of value semantics in structs and provides code snippets for better understanding.
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)
7 views15 pages

Swift Structs vs Classes and Apps Guide

The document compares structs and classes in Swift, highlighting their differences in type, memory allocation, inheritance, and mutability. It also includes examples of a To-Do List app with filtering logic and a BMI Calculator app, demonstrating the use of structs and classes in practical applications. The document emphasizes the importance of value semantics in structs and provides code snippets for better understanding.
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

Written Assignment 6

University of the People

CS 4405 - Mobile Applications

2025/10/15
1. Comparison of Structs and Classes in Swift

Swift offers two primary data constructs: structs and classes. Although they may appear

similar, they differ significantly in terms of behavior and use cases.

Key Differences

Feature Structs Classes

Type Value type Reference type

Copied when assigned or Shared reference when assigned or


Memory Allocation
passed passed

Inheritance Not supported Supported

Mutability Immutable by default Mutable

ARC (Memory Uses Automatic Reference


Not applicable
Management) Counting (ARC)

Lightweight models, value


Use Case Complex models, shared state
semantics

Code Example
// Struct Example

struct PersonStruct {

var name: String

var person1 = PersonStruct(name: "Alice")

var person2 = person1

[Link] = "Bob"

print([Link]) // Output: Alice

// Class Example

class PersonClass {

var name: String

init(name: String) {

[Link] = name

}
var person3 = PersonClass(name: "Alice")

var person4 = person3

[Link] = "Bob"

print([Link]) // Output: Bob

In the struct example, person1 remains unchanged when person2 is modified, demonstrating

value semantics. In contrast, modifying person4 affects person3 because both reference the

same instance (Apple Inc., 2023).

Reference

Apple Inc. (2023). Structures and Classes. Swift Language Guide.

[Link]

classesandstructures/

2. To-Do List App with Filtering Logic

Struct Definition and Filtering Function

import Foundation

struct ToDo {
var toDoId: Int

var toDoTitle: String

var toDoStatus: Bool // true = completed, false = pending

// Sample Data

let toDoList = [

ToDo(toDoId: 1, toDoTitle: "TestSet 1 - Day 1", toDoStatus: true),

ToDo(toDoId: 2, toDoTitle: "TestSet 2 - Day 2", toDoStatus: true),

ToDo(toDoId: 3, toDoTitle: "TestSet 1 - Day 3", toDoStatus: false),

ToDo(toDoId: 4, toDoTitle: "TestSet 2 - Day 4", toDoStatus: true),

ToDo(toDoId: 5, toDoTitle: "TestSet 1 - Day 5", toDoStatus: true),

ToDo(toDoId: 6, toDoTitle: "TestSet 2 - Day 6", toDoStatus: false)

// Filtering Function

func filterCompletedEvenToDos(from list: [ToDo]) -> [ToDo] {


return [Link] { $[Link] % 2 == 0 && $[Link] }

// Usage

let filteredToDos = filterCompletedEvenToDos(from: toDoList)

for todo in filteredToDos {

print("Completed TestSet 2: \([Link])")

This function filters and returns only the completed tasks from even-numbered days, aligning

with the requirement to track TestSet 2 progress.

3. BMI Calculator App in Swift

Business Logic

import UIKit

enum BMIStatus {

case underweight, normal, overweight


}

class Person {

var weight: Double // in kg

var height: Double // in meters

var gender: String

init(weight: Double, height: Double, gender: String) {

[Link] = weight

[Link] = height

[Link] = gender

func calculateBMI() -> Double {

return weight / (height * height)

}
func getBMIStatus() -> BMIStatus {

let bmi = calculateBMI()

switch bmi {

case ..<18.5:

return .underweight

case 18.5..<24.9:

return .normal

default:

return .overweight

SwiftUI Interface

import SwiftUI

struct BMICalculatorView: View {

@State private var weight = ""


@State private var height = ""

@State private var gender = ""

@State private var resultMessage = ""

@State private var resultColor = [Link]

var body: some View {

VStack(spacing: 20) {

TextField("Enter weight (kg)", text: $weight)

.keyboardType(.decimalPad)

TextField("Enter height (m)", text: $height)

.keyboardType(.decimalPad)

TextField("Enter gender", text: $gender)

Button("Calculate BMI") {

if let w = Double(weight), let h = Double(height) {

let person = Person(weight: w, height: h, gender: gender)

let bmi = [Link]()


let status = [Link]()

switch status {

case .underweight:

resultMessage = "You are underweight. BMI: \(String(format: "%.2f", bmi))"

resultColor = .red

case .normal:

resultMessage = "Your weight is normal. BMI: \(String(format: "%.2f", bmi))"

resultColor = .green

case .overweight:

resultMessage = "You are overweight. BMI: \(String(format: "%.2f", bmi))"

resultColor = .red

.padding()
Text(resultMessage)

.foregroundColor(resultColor)

.padding()

This app calculates BMI and displays a color-coded message based on the result. The logic

uses object-oriented principles and enums to determine weight status (Centers for Disease

Control and Prevention [CDC], 2022).


Reference

Centers for Disease Control and Prevention. (2022). About Adult BMI.

[Link]

You might also like