Browse Questions
- URLCache stores responses from network requests in memory and/or on disk.
- UITableView reuses cell objects as they scroll off screen rather than creating new ones, keeping memory usage constant regardless of how many rows exist.
- A property wrapper adds custom logic around getting and setting a property.
- | Problem | Cause | Fix | |---------|-------|-----| | #if SIT always falls through to #else | Flag added to Other Swift Flags instead of Active Compilation Conditions | Move flag to the correct…
Answer:
A property wrapper adds custom logic around getting and setting a property. SwiftUI's @State, @Binding, and @Published are all property wrappers.
Code Example:
@propertyWrapper
struct Clamped {
private var value: Int
let range: ClosedRange<Int>
var wrappedValue: Int {
get { value }
set { value = min(max(newValue, range.lowerBound), range.upperBound) }
}
init(wrappedValue: Int, _ range: ClosedRange<Int>) {
self.range = range
self.value = min(max(wrappedValue, range.lowerBound), range.upperBound)
}
}
struct Player {
@Clamped(0...100) var health: Int = 100
}
var player = Player()
player.health = 150 // clamped to 100
player.health = -10 // clamped to 0