© 2025, A.Voß, FH Aachen, codebasedlearning.dev
Finally, we want to build iOS apps for which we need swift. The language is very comprehensive and we want to focus on what we need for the apps first. So we'll start with a brief overview of a few small apps, and collect the features of the language. We will then look at them one by one.
We must at least discuss these subjects:
- comments
- var, const
- primitive types
- value and reference types
- collections, such as Array
- iterations
- functions and parameters
- functions as first-class citizens, closure variables or properties
- struct, class
- propeties, wrapper, getter, setter, backing fields, $-syntax
- lazy
- private, public
- member functions
- inheritance, protocol
- static
- enums
- trailing closure/lambda
- chaining
- guard
- ?-optionals, Nil-coalescing operator
Create a macOS Command Line Tool (e.g. in 'New Project
MySolutions_PartI
') and single solution functions for each task or use an Online Swift Playground, e.g. https://swiftfiddle.com .
- Solve this task in a function called
theWhisperingIsland
. - Read a text from the console,
- and convert it to an integer
n
. - Print
n
to the console, e.g. "Your number wasn
". Use 'string interpolation'. - Repeat this until you get an empty text.
- Protect your code appropriately with 'optional binding' and 'guards'.
Do you need exceptions?
Hint: readLine() -> String?
returns a string read from standard input through the end of the current line.
- Solve this task in a function called
theMountainOfSilver
. - Read a number
n
from the console, or passn
as a parameter if you know how to read, - and calculate the n'th Fibonacci number, see Fibonacci. once in a loop and once with recursion.
- Add a caching system (called 'memoization') to the recursive computation, e.g. by using a dictionary containing already calculated Fibonacci numbers.
- Use a 'guard' to throw an exception of your type, based on an
enum
derived fromError
, ifn
is not positive. - Test all different variants including the error and memoization-cases.
Hint: n=35 -> fib(n)=9227465
- Solve this task in a function called
theDoomFields
. - Read a number
n
from the console, or passn
as a parameter, and calculate the sequence of numbers according to the Collatz conjecture, see Collatz. - Use a list that you fill in one after the other and print it at the end.
Hint: n=19 -> collatz=[19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]
- Solve this task in a function called
theMistLabyrinth
. - Write a function
findDigit
to find the first or the last digit in a parameterline
of type string. A second parameterfromStart
of type bool states whether you want to search from the start or from the end. Return an optional character which is the found character ornil
. - Write a function
findNumber
, which again takes a line and returns a combination of the first and last digit as a number (if found) or0
. - Example, the values of these five lines are 34, 50, 0, 88, and 33:
3POeQx4
h5g6srvOA70N
abcdef
f8BMx7BgE8y
rLHp3jcN
- Put the text above in a raw string, split it into lines, call
findNumber
for each line and print the line and the result.
Hint: c.isNumber
indicates whether a character c
represents a number.
- Solve this task in a function
theHarborOfChaos
. - Assume the coefficients of a polynomial are stored in an array, e.g.
[1,2,3]
means1+2x+3x^2
. Write a functionapply
which receives an array (the polynomial)p
, a valuex
, and a closurem
, mapping Double to Double. It calculatesy=p(x)
using the Horner scheme, and returnsm(y)
. - Test
apply
with the polynomial above,x=2.0
andm:x->x^2
.
Hint: Consider reduce
, and m(p(2.0))=289.0
.
Extend your project from Part I.
Develop a flexible and extensible payment processing system. The system should accommodate various payment methods including credit cards, PayPal, and bank transfers.
- Solve this task in a function
theBrokenVillage
. - Define a
PaymentProtocol
that includes the following:- A member function
authorize
without parameters. It authorizes a payment and returns a boolean indicating success. - A member function
completePayment
without parameters. It completes the payment if authorization is successful and returns a boolean indicating success. - A read-only property
amount
of typeDouble
to hold the amount to be paid.
- A member function
- Create a base class
Payment
that implementsPaymentProtocol
and default behaviors for the methods declared in the protocol (print a message with theamount
). - Implement a subclass
CreditCardPayment
for handling credit card transactions. Add properties relevant to credit card payments, such ascreditCardNumber
and override one method. - Implement a subclass
PayPalPayment
for handling payments through PayPal. Include properties specific to PayPal payments, such asaccountEmail
and override the other method. - Create instances of each payment subclass, store them in a list and execute both payment processes traversing this list.
Implement a Swift extension that converts an integer to its corresponding Roman numeral representation.
- Solve this task in a function
theHarborOfExiles
. - Extend the
Int
type with a computed propertyroman
that returns the Roman numeral equivalent as aString?
. - The Roman numeral should only be computed for positive integers. Return
nil
for zero or negative integers. - Ensure the conversion adheres to the rules of Roman numerals:
- Symbols include I (1), V (5), X (10), L (50), C (100), D (500), M (1000).
- Implement rules for subtractive notation such as IV (4), IX (9), etc.
- Test the extension with a range of numbers to ensure accurate conversion. Include edge cases such as very small numbers, relatively large numbers, and invalid inputs like zero or negative numbers.
Develop a generic Queue class that can handle any data type. The queue should support basic operations such as enqueue, dequeue, and peek.
- Solve this task in a function
theAgonyLands
. - Implement a generic class
Queue
where the type of elements it holds is unspecified until an instance is created. The queue should be able to store elements of any given type. Implement these Queue Operations:enqueue
: Add an element to the back of the queue.dequeue
: Remove the element at the front of the queue and return it. If the queue is empty, returnnil
.peek
: Return the element at the front of the queue without removing it. If the queue is empty, returnnil
.isEmpty
: A property or method that returns a boolean indicating whether the queue is empty.
- Use an array to store the elements of the queue. Ensure that the queue operations are as efficient as possible, considering the underlying array's performance characteristics.
- Create instances of the queue with different data types, e.g.,
Int
,String
, and a customstruct
orclass
. Perform a series of operations to demonstrate that the queue handles enqueuing, dequeuing, and peeking correctly. Test edge cases such as dequeueing from an empty queue.
- If you have not completed the main tasks in the exercise, complete them at home.
End of Tasks