Conversation
Added XC Playground file for Exercises 1-3
ericjenkinson
left a comment
There was a problem hiding this comment.
Excellent work! I find it great that you are taking the time to experiment and add to the exercise.
Meets expectations!
| } | ||
|
|
||
| // Calling the main function | ||
| main() |
There was a problem hiding this comment.
Many languages require some form of entry point to be defined that instructs the compiler where execution should start. That is not the case with Swift, especially in Playgrounds. In Playground execution, always start at the top of the file and continue line by line until the end of the file. Lines 27 through 35 do not need to be wrapped in a function. This applies to the "main functions" below as well.
| } | ||
| } | ||
|
|
||
| //MARK: - DiscountStrategy Protocol and Implementations |
There was a problem hiding this comment.
I like seeing that you are experimenting with the concepts you are learning.
|
|
||
| class ShoppingCartSingleton { | ||
| // Static property for the singleton instance | ||
| private static var instance: ShoppingCartSingleton? |
There was a problem hiding this comment.
If you are using Xcode 16, you may have received the error: Static property 'instance' is not concurrency-safe because it is nonisolated global shared mutable state. Swift 6 introduced strict concurrency checking so this error will not be present on an older version of Xcode.
With this error, the compiler is trying to communicate that Static mutable properties are generally unsafe because they can be concurrently modified from any thread/actor. Will go over concurrency later in the course, for now since instance is only accessed by the ShoppingCartSingleton class we can add nonisolated(unsafe) to the definition.
private static nonisolated(unsafe) var instance: ShoppingCartSingleton?
| print("Remaining cash in register: $\(cashInRegister)") | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
You missed adding the code to try processing payments with different processors and handle potential errors using try-catch blocks.
|
|
||
| // Method to calculate the total price | ||
| func getTotalPrice() -> Double { | ||
| let subtotal = products.reduce(0.0) { $0 + ($1.price * Double($1.quantity)) } |
There was a problem hiding this comment.
Excellent use of higher order methods!
| } | ||
|
|
||
| let subtotal = products.reduce(0.0) { $0 + ($1.price * Double($1.quantity)) } | ||
| let discount = discountStrategy.calculateDiscount(total: subtotal) |
There was a problem hiding this comment.
Could you use the getTotalPrice() method instead?
Added XC Playground file for Exercises 1-3