lOMoARcPSD|56775388
CS 4405 - Written Assignment Unit 6
Mobile Applications (University of the People)
Scan to open on Studocu
Studocu is not sponsored or endorsed by any college or university
Downloaded by Arthur Balibuno (arthurbalibunontwali@gmail.com)
lOMoARcPSD|56775388
Written Assignment Unit 6
CS 4405 – Mobile Applications
Topic: Object Oriented Programming in Swift and iOS User Interface
Date: December 23, 2024.
Part 1:
In Swift, structs and classes are versatile types with distinct features. Below is a detailed
comparison with examples:
1. Value Type vs. Reference Type
Structs are value types, meaning they are copied when assigned or passed to a
function.
Classes are reference types, meaning they are passed by reference (The swift
programming language swift 5.7, n.d).
Example:
struct StructExample {
var value: Int
}
class ClassExample {
var value: Int
}
var struct1 = StructExample(value: 10)
var struct2 = struct1
struct2.value = 20
print(struct1.value) // Output: 10 (original struct unchanged)
var class1 = ClassExample()
class1.value = 10
var class2 = class1
Downloaded by Arthur Balibuno (arthurbalibunontwali@gmail.com)
lOMoARcPSD|56775388
class2.value = 20
print(class1.value) // Output: 20 (class1 reflects change)
2. Memory Management
Structs are stored on the stack, which is faster and automatically managed.
Classes are stored on the heap and require reference counting (ARC).
3. Mutability
Structs require explicit mutability with the mutating keyword for methods that
modify properties.
Classes allow modification of properties freely if they are declared as var (The swift
programming language swift 5.7, n.d).
Example:
struct StructWithMutating {
var value: Int
mutating func increment() {
value += 1
}
}
class ClassWithoutMutating {
var value: Int = 0
func increment() {
value += 1
}
}
var myStruct = StructWithMutating(value: 10)
myStruct.increment()
Downloaded by Arthur Balibuno (arthurbalibunontwali@gmail.com)
lOMoARcPSD|56775388
print(myStruct.value) // Output: 11
let myClass = ClassWithoutMutating()
myClass.increment()
print(myClass.value) // Output: 1
4. Inheritance
Structs do not support inheritance.
Classes support inheritance, allowing child classes to extend parent functionality (The
swift programming language swift 5.7, n.d).
Example:
class ParentClass {
var name: String = "Parent"
}
class ChildClass: ParentClass {
var age: Int = 10
}
let child = ChildClass()
print(child.name) // Output: Parent
5. Identity
Structs do not have identity; each instance is independent.
Classes have identity; multiple references point to the same instance.
Example:
class IdentityClass {
var name: String
init(name: String) {
self.name = name
Downloaded by Arthur Balibuno (arthurbalibunontwali@gmail.com)
lOMoARcPSD|56775388
}
}
let instance1 = IdentityClass(name: "Alice")
let instance2 = instance1
instance2.name = "Bob"
print(instance1.name) // Output: Bob
6. Use Cases
Use structs for lightweight, immutable data types or value semantics (e.g., Point,
Rectangle).
Use classes for complex objects requiring inheritance or shared state (e.g.,
ViewController, DatabaseManager) (Ching, 2021).
Summary Table
Feature Struct Class
Type Value type Reference type
Memory Stack Heap
Inheritance Not supported Supported
Mutability Explicit with mutating Implicit
Performance Faster for lightweight data Overhead due to ARC
Identity No identity Has identity
This distinction helps decide whether to use a struct or a class in your project depending on
the requirements (The swift programming language swift 5.7, n.d).
Part 2:
To Do Class
Downloaded by Arthur Balibuno (arthurbalibunontwali@gmail.com)
lOMoARcPSD|56775388
Downloaded by Arthur Balibuno (arthurbalibunontwali@gmail.com)
lOMoARcPSD|56775388
Downloaded by Arthur Balibuno (arthurbalibunontwali@gmail.com)
lOMoARcPSD|56775388
Part 3
Person Model
Downloaded by Arthur Balibuno (arthurbalibunontwali@gmail.com)
lOMoARcPSD|56775388
Downloaded by Arthur Balibuno (arthurbalibunontwali@gmail.com)
lOMoARcPSD|56775388
Output
Downloaded by Arthur Balibuno (arthurbalibunontwali@gmail.com)
lOMoARcPSD|56775388
Downloaded by Arthur Balibuno (arthurbalibunontwali@gmail.com)
lOMoARcPSD|56775388
Downloaded by Arthur Balibuno (arthurbalibunontwali@gmail.com)
lOMoARcPSD|56775388
Reference
Ching, C. (2021, April 19). The definitive Swift tutorial for beginners. CodeWithChris.
https://codewithchris.com/swift-tutorial-complete/
The swift programming language swift 5.7. (n.d.). Apple Inc. https://docs.swift.org/swift-
book/LanguageGuide/TheBasics.html
Downloaded by Arthur Balibuno (arthurbalibunontwali@gmail.com)