Explain it simply
Properties, in plain language
Properties are the variables and constants that hold data inside a struct/class/enum. A stored property actually holds the data. A computed property, on the other hand, calculates its value based on other data.
Problem → Why → Solution
The problem — why this concept matters
If a computed property's logic ends up calling itself, you'll get infinite recursion.
A common mistake
Assuming one sample output proves nil, ownership, actor isolation, and every error path.
Core concept — understand how it works
Treat Properties as a contract about value and reference semantics, mutability, copying, identity, optionality, and lifetime—not merely syntax. Prefer struct or enum value types and use class identity or shared mutable state only when required.
- typed value
- Swift expression or function
- safe result
Explain the type, ownership, and runtime behavior of Properties
Core syntax or pattern
struct Product {
var name: String
var price: Double
var discountPercent: Double
var finalPrice: Double {
price - (price * discountPercent / 100)
}
}
let plan = Product(name: "Pro Plan", price: 20.0, discountPercent: 10)
print("\(plan.name): $\(plan.finalPrice)")Treat Properties as a contract about value and reference semantics, mutability, copying, identity, optionality, and lifetime—not merely syntax. Prefer struct or enum value types and use class identity or shared mutable state only when required.
Read the code step by step
- typed value
struct Product {Properties are the variables and constants that hold data inside a struct/class/enum. A stored property actually holds the data. A computed property, on the other hand, calculates its value based on other data.
- Swift expression or function
price - (price * discountPercent / 100)Treat Properties as a contract about value and reference semantics, mutability, copying, identity, optionality, and lifetime—not merely syntax. Prefer struct or enum value types and use class identity or shared mutable state only when required.
- safe result
print("\(plan.name): $\(plan.finalPrice)")Run and test an original Properties example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
- typed value
- Swift expression or function
- safe result
Success
Explain the type, ownership, and runtime behavior of Properties
Failure
If a computed property's logic ends up calling itself, you'll get infinite recursion.
Properties are the variables and constants that hold data inside a struct/class/enum. A stored property actually holds the data. A computed property, on the other hand, calculates its value based on other data.
struct Product {
var name: String
var price: Double
var discountPercent: Double
var finalPrice: Double {
price - (price * discountPercent / 100)
}
}
let plan = Product(name: "Pro Plan", price: 20.0, discountPercent: 10)
print("\(plan.name): $\(plan.finalPrice)")name, price, discountPercent are stored properties. finalPrice is a computed property that calculates the discount and returns the result.
Pro Plan: $18.0Real-world usage
How to apply it in a real project
Run and test an original Properties example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
Mini challenge
Build it yourself
Run and test an original Properties example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
struct Product {
var name: String
var price: Double
var discountPercent: Double
var finalPrice: Double {
price - (price * discountPercent / 100)
}
}
let plan = Product(name: "Pro Plan", price: 20.0, discountPercent: 10)
print("\(plan.name): $\(plan.finalPrice)")Expected: Explain the type, ownership, and runtime behavior of Properties
Show hint
Run and test an original Properties example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- typed value → Swift expression or function → safe result
- Treat Properties as a contract about value and reference semantics, mutability, copying, identity, optionality, and lifetime—not merely syntax. Prefer struct or enum value types and use class identity or shared mutable state only when required.
- If a computed property's logic ends up calling itself, you'll get infinite recursion.
- Run and test an original Properties example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
The Swift Programming Language — Swift.org