Properties: Basics to Advanced Challenges

Swift Challenges

Abdullah Bilgin
Swift Insights
3 min readJan 6, 2024

--

Introduction:

Welcome to Mastering Swift Properties, a journey designed to deepen your understanding of Swift properties through a series of challenges. Whether you’re a beginner eager to grasp the basics or an experienced developer seeking advanced insights, these challenges cover a spectrum of property-related concepts. In this exploration, we’ll tackle each challenge head-on, providing solutions and in-depth explanations to unravel the intricacies of Swift properties.

Basic Challenges: (Stored Properties)

1. Employee Information: Create an Employee class with stored properties for employee ID, name, and salary. Implement a method to display the employee’s information.

2. Rectangle Properties: Design a Rectangle class with stored properties for length and width. Calculate and print the perimeter of the rectangle.

3. Temperature Conversion: Develop a TemperatureConverter class with stored properties for Celsius and Fahrenheit temperatures. Implement methods to convert between the two units.

4. Product Inventory: Build a Product class with stored properties for product name, price, and quantity in stock. Create a method to calculate the total value of the product inventory.

5. Vehicle Details: Define a Vehicle class with stored properties for make, model, and year. Include a method to print the vehicle details.

Intermediate Challenges: (Computed Properties)

6. Square Properties: Extend the Rectangle class from the basic challenges to include a computed property for the area of the square, if applicable.

7. Bank Account Transactions: Implement a BankAccount class with stored properties for account balance and an array of transactions. Create a computed property to calculate the total balance considering all transactions.

8. Email Validation: Design an EmailValidator class with a stored property for the email address. Implement a computed property that validates if the email address is correctly formatted.

9. User Authentication: Develop a User class with stored properties for username and password. Create a computed property that checks the strength of the password.

10. Circle Intersection: Enhance the Circle class to include computed properties that determine if two circles intersect and calculate the area of intersection.

Advanced Challenges: (Property Observers and Advanced Computed Properties)

11. Real-time Stock Prices: Create a Stock class with a stored property for the current stock price. Implement a property observer to track changes and a computed property to calculate the percentage change.

12. Custom Date Formatting: Extend the Employee class from the basic challenges. Add a stored property for the birthdate and a computed property to format the birthdate in a custom way.

13. Database Integration: Build a DatabaseModel class with a stored property for the data model. Implement a property observer to update a database whenever the model is modified.

14. Multi-step Transaction Approval: Design a Transaction class with stored properties for the transaction amount and approval status. Use property observers to implement a multi-step approval process.

15. Smart Home Automation: Develop a SmartHomeDevice class with stored properties for device status and power consumption. Use property observers to trigger actions based on changes in the device status.

These challenges cover a wide range of Swift property concepts, from basic stored properties to advanced scenarios involving property observers and complex computed properties. They provide a holistic understanding of how properties can be utilized in various real-world situations. Happy coding!

Solutions:

// Challenge 1: Employee Information
class Employee {
var employeeID: Int
var name: String
var salary: Double

init(employeeID: Int, name: String, salary: Double) {
self.employeeID = employeeID
self.name = name
self.salary = salary
}

func displayInformation() {
print("Employee ID: \(employeeID), Name: \(name), Salary: \(salary)")
}
}

// Testing the challenges
let employee = Employee(employeeID: 1, name: "John Doe", salary: 50000)
employee.displayInformation()

For more challenges and solutions, check out the complete Swift Playground on my GitHub:

--

--

Abdullah Bilgin
Swift Insights

"iOS engineer & IT head, crafting code & innovation. Leading with tech prowess & strategic vision. Bridging iOS dev & IT realms. Lifelong learner. 📱💡"