Bird
Raised Fist0
Swiftprogramming~20 mins

WrappedValue and projectedValue in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift Property Wrapper Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Swift property wrapper example?
Consider this Swift code using a property wrapper. What will be printed when accessing user.name and user.$name?
Swift
import Foundation

@propertyWrapper
struct Capitalized {
    private var value: String = ""
    var wrappedValue: String {
        get { value }
        set { value = newValue.capitalized }
    }
    var projectedValue: String {
        "Projected: \(value)"
    }
}

struct User {
    @Capitalized var name: String
}

var user = User()
user.name = "john doe"
print(user.name)
print(user.$name)
A
John Doe
Projected: John Doe
B
john doe
Projected: John Doe
C
John Doe
john doe
D
john doe
Projected: john doe
Attempts:
2 left
💡 Hint
Remember that wrappedValue applies capitalization, and projectedValue returns a string with prefix.
🧠 Conceptual
intermediate
1:30remaining
What does the projectedValue property provide in a Swift property wrapper?
In Swift property wrappers, what is the main purpose of the projectedValue property?
AIt replaces the wrappedValue property for reading and writing.
BIt stores the original unmodified value before wrapping.
CIt automatically validates the wrapped value before setting it.
DIt provides an alternative interface to access additional functionality related to the wrapped property.
Attempts:
2 left
💡 Hint
Think about how you access the property with a $ prefix.
🔧 Debug
advanced
2:30remaining
Why does this Swift code cause a compile error?
Examine this property wrapper code. Why does it cause a compile error? @propertyWrapper struct Wrapper { var wrappedValue: Int var projectedValue: Int { wrappedValue * 2 } } struct Example { @Wrapper var number: Int } let e = Example(number: 5) print(e.$number)
Swift
import Foundation

@propertyWrapper
struct Wrapper {
    var wrappedValue: Int
    var projectedValue: Int {
        wrappedValue * 2
    }
}

struct Example {
    @Wrapper var number: Int
}

let e = Example(number: 5)
print(e.$number)
ABecause the struct Example does not declare number as optional.
BBecause the property wrapper lacks an initializer to set wrappedValue.
CBecause wrappedValue cannot be an Int type.
DBecause projectedValue must be a function, not a computed property.
Attempts:
2 left
💡 Hint
Property wrappers need an initializer if wrappedValue is not optional or has no default.
📝 Syntax
advanced
1:30remaining
Which option correctly defines a projectedValue in a Swift property wrapper?
Choose the correct syntax for defining a projectedValue property in a Swift property wrapper that returns a String.
Avar projectedValue: String { "Value is \(wrappedValue)" }
Bfunc projectedValue() -> String { return "Value is \(wrappedValue)" }
Cvar projectedValue: String() { "Value is \(wrappedValue)" }
Dlet projectedValue: String = "Value is \(wrappedValue)"
Attempts:
2 left
💡 Hint
projectedValue is a computed property, not a method or constant.
🚀 Application
expert
3:00remaining
How many items are in the dictionary after this Swift code runs?
Given this Swift property wrapper and usage, how many key-value pairs are in the dictionary counts after execution?
Swift
@propertyWrapper
struct Counted {
    private static var counts = [String: Int]()
    private let key: String
    var wrappedValue: String {
        didSet {
            Self.counts[key, default: 0] += 1
        }
    }
    var projectedValue: Int {
        Self.counts[key] ?? 0
    }
    init(wrappedValue: String, key: String) {
        self.wrappedValue = wrappedValue
        self.key = key
        Self.counts[key] = 0
    }
}

struct Test {
    @Counted(key: "a") var first = "Hello"
    @Counted(key: "b") var second = "World"
}

var t = Test()
t.first = "Hi"
t.second = "Earth"
t.first = "Hey"
A4
B3
C2
D1
Attempts:
2 left
💡 Hint
Each key is unique and increments count on each wrappedValue change.

Practice

(1/5)
1. What does wrappedValue represent in a Swift property wrapper?
easy
A. The main value stored or controlled by the property wrapper
B. An extra method to modify the property
C. A way to access the property's type information
D. A function to reset the property to default

Solution

  1. Step 1: Understand the role of wrappedValue

    The wrappedValue is the core value that the property wrapper manages or stores internally.
  2. Step 2: Differentiate from other features

    Unlike projectedValue, wrappedValue is the actual data, not extra info or behavior.
  3. Final Answer:

    The main value stored or controlled by the property wrapper -> Option A
  4. Quick Check:

    wrappedValue = main stored value [OK]
Hint: wrappedValue is the property's actual stored value [OK]
Common Mistakes:
  • Confusing wrappedValue with projectedValue
  • Thinking wrappedValue is a method
  • Assuming wrappedValue holds extra metadata
2. Which syntax correctly accesses the projectedValue of a property wrapper named score?
easy
A. score.projectedValue
B. $score
C. score.$projectedValue
D. score->projectedValue

Solution

  1. Step 1: Recall how to access projectedValue

    In Swift, the projectedValue is accessed using the dollar sign prefix before the property name.
  2. Step 2: Match syntax to property name

    For a property named score, the projectedValue is accessed as $score.
  3. Final Answer:

    $score -> Option B
  4. Quick Check:

    projectedValue = $propertyName [OK]
Hint: Use $ before property name to get projectedValue [OK]
Common Mistakes:
  • Trying to access projectedValue as a normal property
  • Using dot syntax incorrectly
  • Confusing $score with score.$projectedValue
3. Given this code, what will be printed?
 @propertyWrapper
 struct Capitalized {
     private var value: String = ""
     var wrappedValue: String {
         get { value }
         set { value = newValue.capitalized }
     }
     var projectedValue: String {
         "Length: \(value.count)"
     }
 }

 struct Person {
     @Capitalized var name: String
 }

var p = Person()
p.name = "john doe"
print(p.name)
print(p.$name)
medium
A. John Doe Length: 8
B. john doe Length: 8
C. John Doe Length: 7
D. john doe Length: 7

Solution

  1. Step 1: Understand wrappedValue behavior

    Setting p.name to "john doe" triggers the setter, which capitalizes the string to "John Doe".
  2. Step 2: Calculate projectedValue output

    The projectedValue returns the length of the capitalized string. "John Doe" has 8 characters including the space.
  3. Final Answer:

    John Doe\nLength: 8 -> Option A
  4. Quick Check:

    wrappedValue capitalizes, projectedValue shows length [OK]
Hint: wrappedValue changes value, projectedValue gives extra info [OK]
Common Mistakes:
  • Counting characters without space
  • Ignoring capitalization effect
  • Using p.name instead of p.$name for projectedValue
4. Identify the error in this property wrapper usage:
 @propertyWrapper
 struct Trimmed {
     var wrappedValue: String {
         didSet { wrappedValue = wrappedValue.trimmingCharacters(in: .whitespaces) }
     }
 }

 struct User {
     @Trimmed var username: String
 }

var u = User()
u.username = "  alice  "
print(u.username)
medium
A. wrappedValue cannot be mutated inside didSet
B. Cannot use didSet in property wrapper
C. Missing initial value for wrappedValue
D. Property wrapper must be a class, not struct

Solution

  1. Step 1: Check wrappedValue declaration

    The wrappedValue property lacks an initial value or backing storage, causing a compile error.
  2. Step 2: Understand property wrapper requirements

    Property wrappers must have a stored wrappedValue or initializer to set it; here it's missing.
  3. Final Answer:

    Missing initial value for wrappedValue -> Option C
  4. Quick Check:

    wrappedValue needs initial value or storage [OK]
Hint: wrappedValue must have storage or initializer [OK]
Common Mistakes:
  • Trying to mutate wrappedValue inside didSet
  • Assuming didSet works without storage
  • Confusing struct vs class requirement
5. You want to create a property wrapper that stores an integer and also tracks how many times the value was changed. Which approach correctly uses wrappedValue and projectedValue to achieve this?
hard
A. Use projectedValue only to store and access the integer
B. Use projectedValue to store the integer and wrappedValue to return the change count
C. Use wrappedValue for both storing integer and change count
D. Use wrappedValue to store the integer and projectedValue to return the change count

Solution

  1. Step 1: Define roles of wrappedValue and projectedValue

    wrappedValue should hold the main integer value; projectedValue can provide extra info like change count.
  2. Step 2: Match roles to requirements

    Tracking changes is extra info, so projectedValue is ideal to expose the count without mixing with main value.
  3. Final Answer:

    Use wrappedValue to store the integer and projectedValue to return the change count -> Option D
  4. Quick Check:

    wrappedValue = main data, projectedValue = extra info [OK]
Hint: wrappedValue stores data, projectedValue tracks extra info [OK]
Common Mistakes:
  • Swapping roles of wrappedValue and projectedValue
  • Trying to store multiple values in wrappedValue
  • Using projectedValue as main storage
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard