![]() |
Advanced Programming in Swift | Coursera Meta |
In the core of every exceptional iOS application lies a deep grasp of the Swift programming language. To advance your expertise, enroll in Advanced Programming in Swift. Delve into Swift's advanced custom data types, refine code organization strategies, master error handling for enhanced program efficiency, and delve into functional programming principles through higher-order functions such as map, filter, and reduce. Additionally, gain insights into implementing unit tests to ensure the robust functionality of your applications.
By completing this course, you'll gain hands-on experience in building functionalities commonly found in apps with extensive item lists. You'll explore advanced programming principles, including employing higher-order functions for collection processing and constructing lists within Xcode. Upon finishing the course, you'll be equipped to:
- Develop custom data types, including enumerations and sets.
- Efficiently organize and optimize code using subclassing, inheritance, typecasting, and polymorphism.
- Implement access control to regulate code restrictions.
- Create flexible code structures using optional and required protocols.
- Utilize delegation to transfer control and responsibilities between instances.
- Implement robust error handling techniques, including throwable functions and error catching.
- Understand recursion and its practical applications.
- Apply higher-order functions like map, filter, and reduce effectively.
- Ensure application reliability through comprehensive unit testing.
This course targets intermediate learners aiming to prepare for a career in iOS development, requiring a solid foundation in Swift and SwiftUI fundamentals for successful participation.
Notice!
Always refer to the module on your course for the most accurate and up-to-date information.
Attention!
If you have any questions that are not covered in this post, please feel free to leave them in the comments section below. Thank you for your engagement.
Module quiz: Advanced data types
var cities: Set = ["Cairo", "London", "Paris"]var cities2: Set<String> = ["Moscow", "Hanoi", "Zurich"]
- True
- False
var numbersA : Set = [100, 102, 103]var numbersB : Set = [101, 103, 100]let numbers = numbersA.union(numbersB)print(numbers)
- [100, 101, 103, 101]
- [100, 100, 102, 103]
- [103, 102, 103, 101]
- [100, 102, 103, 101]
enum Week: Int, CaseIterable {case Monday = 1case Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}for day in Week.allCases {print("\(day) is day \(day.rawValue) of the week")}
- No error but no output
- An error is thrown because only Monday has a raw value.
-
Monday is day 1 of the week
Tuesday is day 2 of the week
Wednesday is day 3 of the week
Thursday is day 4 of the week
Friday is day 5 of the week
Saturday is day 6 of the week - Monday is day 1 of the week
- Functions
- Raw values
- Computed properties
enum PastaTypes {case spaghetticase pennecase raviolicase rigatoni}
- Double
- String
- Int
- None of the above
enum PastaTypes: Int {case spaghetticase pennecase raviolicase rigatoni}Print(PastaTypes.penne.rawValue)
- 0
- 1
- 2
- None of the above
- CaseIterable
- Equatable
- Hashable
var listOfNumbers = Set<Int>()listOfNumbers.insert(1)listOfNumbers.insert(5)listOfNumbers.insert(8)listOfNumbers.insert(3)ListOfNumbers.insert(1)print(listOfNumbers.count)
- 5
- 4
- None of the above.
- Contains elements that have key-value pairs where each key must be unique.
- Can only store distinct unordered values of the same type.
- Contains elements in a collection that don’t necessarily need to be unique.
- Array
- Set
Module quiz: Code organization
class Vehicle {}
- True
- False
class Vehicle {}class Truck: Vehicle {}
- Truck
- Vehicle
class SecretFood {private var secretIngredients: [String] = []}class Chef {func cookSecretFood(_ secretFood: SecretFood) {print(secretFood.secretIngredients)}}
- Yes
- No
- private, fileprivate, internal, public, open
- internal, private, fileprivate, public, open
- open, public, internal, fileprivate, private
- as?
- ==
- as!
- is
class Spaghetti {func fetchIngredients() {print("Spaghetti Ingredients")}}class SpaghettiMeatball: Spaghetti {override func fetchIngredients() {print("BBB")super.fetchIngredients()print("AAA")}}let spaghettiMeatball = SpaghettiMeatball()spaghettiMeatball.fetchIngredients()
- AAA
BBB
Spaghetti Ingredients -
BBB
Spaghetti Ingredients
AAA - Spaghetti Ingredients
- Spaghetti Ingredients
BBB
AAA
- let propertyIdentifier: Int
- var propertyIdentifier: Int { set }
- var propertyIdentifier: Int { get }
- func methodIdentifier() { get }
- func methodIdentifier() -> String {}
- func methodIdentifier() -> String
protocol Employee {var daysWorking: Int { get set }}
struct Waiter: Employee {var daysWorking: Int}
struct Waiter: Employee {var daysWorking: Double}
struct Waiter: Employee {let daysWorking: Int}
- 0
- 1
- Unlimited
protocol ProtocolIdentifier {func methodIdentifier(parameter: Int) -> Intvar propertyIdentifier: Int { get }}
@objc protocol ProtocolIdentifier {@objc optional func methodIdentifier(parameter: Int) -> Intvar propertyIdentifier: Int? { get }}
@objc protocol ProtocolIdentifier {func methodIdentifier(parameter: Int) -> Int@objc optional var propertyIdentifier: Int { get }}
@objc protocol ProtocolIdentifier {@objc func methodIdentifier(parameter: Int) -> Intvar propertyIdentifier: Int { get }}
protocol ProtocolIdentifier {func methodIdentifier(parameter: Int) -> Int@objc optional var propertyIdentifier: Int { get }}
protocol Driver {var name: String { get }func driveToDestination(_ destination: String, with food: String)}class DeliveryDriver: Driver {let name: Stringinit(name: String) {self.name = name}func driveToDestination(_ destination: String, with food: String) {print("\(name) is driving to \(destination) to deliver \(food).")}}class LittleLemon {var deliveryDriver: Driver?func getDriverName() {if let name = deliveryDriver?.name {print("Driver name: \(name)")} else {print("No delivery driver found.")}}}let elisa = DeliveryDriver(name: "Elisa")let littleLemon = LittleLemon()littleLemon.deliveryDriver = elisalittleLemon.getDriverName()
- No delivery driver found.
- Driver name: Elisa
- True
- False
Module quiz: Error handling, functional programming and testing
- It can be used to throw an error from a throwable function.
- Function can be marked with it to make it a throwable-function.
- It is used to declare an error.
do {print("hello")}catch {print("Error caught")}catch {print("Another error caught")}
- Yes, code runs with no issues
- Yes, but first catch block will be executed.
- No, there cannot be two catch blocks after each other.
if let result = try! throwableFunction() {}
- Throwable function must be called inside a do-catch statement.
- Nothing wrong with the code.
- try! Cannot be in if-let statement as it does not return an optional value.
class MyClass: NSError {}
- No, as classes cannot be thrown as errors.
- No, as it does not extend Error protocol.
- Yes
func output() {defer { print(1) }print("2")defer { print(3) }print(4)}
- 4
3
2
1 - 1
2
3
4 -
2
4
3
1
- When a function has a call to itself in the end of its implementation.
- It’s when function has multiple call to itself in its implementation.
- When a function has a call to itself in the beginning of its implementation.
- throwable function
- do-catch
- closure
- forEach
- map
- reduce
- Unit testing is evaluating user interface
- Unit test is verifying a small pieces of code do what they are supposed to do.
- Unit testing is evaluating the compatibility of software across multiple different devices.
- True
- False
Final graded quiz: Advanced Programming in Swift
- struct
- class
- enum
class Wallet {let dollars: Double? = nil}
class Wallet {var dollars: Double = “10”}
class Wallet {let dollars: Double?}
class Wallet {let dollars: Doubleinit(_ dollars: Double) {dollars = dollars}}
let prices = [2.99,2.99,5.99,5.99,5.99,17.99]print(Set(prices))
- None
- [2.99, 2.99, 5.99, 5.99, 5.99, 17.99]
- [5.99, 17.99, 2.99]
- open
- private
- internal
- public
- fileprivate
protocol FoodDelivery {func deliverFood()}struct Car: FoodDelivery {func deliverFood() {print("Deliver food by car")}}class Restaurant {var delegate: FoodDelivery?func delegateDelivery() {if let delegate = delegate {delegate.deliverFood()return}print("No delegate found.")}}
let restaurant = Restaurant()restaurant.delegate = Car()restaurant.delegate?.deliverFood()
let restaurant = Restaurant()restaurant.delegateDelivery()
let restaurant = Restaurant()restaurant.delegate?.deliverFood()restaurant.delegate = Car()
let restaurant = Restaurant()restaurant.delegate = Car()restaurant.delegateDelivery()
let berries = ["strawberry","blueberry","grape","goji"]let result = berries.filter { $0.count > 5 }.map { "healthy \($0)\n" }.reduce("Berries:\n") { $0 + $1 }print(result)
- healthy strawberry
healthy blueberry - Berries:
strawberry
blueberry
grape
goji -
Berries:
healthy strawberry
healthy blueberry
healthy grape
healthy goji - strawberry
blueberry
grape
goji -
Berries:
healthy strawberry
healthy blueberry
class Berry {}class Blueberry: Berry {}class Strawberry: Berry {}let berries = [Berry(), Blueberry(), Strawberry()]for berry in berries {if berry is Berry {print("Berry")}if berry is Blueberry {print("Blueberry")}if berry is Strawberry {print("Strawberry")}}
- Berry
Berry
Berry
Blueberry - Berry
Blueberry
Strawberry -
Berry
Berry
Blueberry
Berry
Strawberry
func test() {let sum = 8 + 7 + 5XCTAssertEqual(sum, 20)}
func test() {XCTAssert(str.isEmpty)let str = "hello"}
func test() {var indices: [Int] = []for i in 0..<5 {indices.append(i)}XCTAssert(indices.count == 4)}
func test() {let product = 10 * 8XCTAssertEqual(product, 80)}