0
0
Swiftprogramming~10 mins

WrappedValue and projectedValue in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to access the wrapped value of the property wrapper.

Swift
struct User {
    @Logged var name: String

    func printName() {
        print(name[1])
    }
}
Drag options to blanks, or click blank then click option'
A.wrappedValue
B.projectedValue
C()
D[0]
Attempts:
3 left
💡 Hint
Common Mistakes
Using .projectedValue instead of .wrappedValue
Trying to call the property as a function
Using array indexing syntax
2fill in blank
medium

Complete the code to access the projected value of the property wrapper.

Swift
struct User {
    @Logged var name: String

    func printLog() {
        print($name[1])
    }
}
Drag options to blanks, or click blank then click option'
A.projectedValue
B.wrappedValue
C()
D[0]
Attempts:
3 left
💡 Hint
Common Mistakes
Using .wrappedValue instead of .projectedValue
Forgetting the $ prefix
Trying to call the property as a function
3fill in blank
hard

Fix the error in accessing the wrapped value inside the property wrapper struct.

Swift
@propertyWrapper
struct Logged {
    private var value: String

    var [1]: String {
        get { value }
        set { value = newValue }
    }

    init(wrappedValue: String) {
        self.value = wrappedValue
    }
}
Drag options to blanks, or click blank then click option'
AprojectedValue
BwrappedValue
Cvalue
Dwrapped
Attempts:
3 left
💡 Hint
Common Mistakes
Using projectedValue instead of wrappedValue
Using a different property name like value or wrapped
4fill in blank
hard

Fill both blanks to define the projectedValue property that returns a log string.

Swift
@propertyWrapper
struct Logged {
    private var value: String
    private(set) var log: String = ""

    var wrappedValue: String {
        get { value }
        set {
            value = newValue
            log += "Value changed to \(newValue)\n"
        }
    }

    var [1]: String {
        return log
    }
}
Drag options to blanks, or click blank then click option'
Alog
BwrappedValue
CprojectedValue
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Naming the property wrappedValue instead of projectedValue
Returning value instead of log
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps property names to their wrapped values if the value is not empty.

Swift
struct User {
    @Logged var firstName: String
    @Logged var lastName: String

    func nonEmptyProperties() -> [String: String] {
        [
            "firstName": $firstName[1],
            "lastName": $lastName[2]
        ].filter { $0.value [3] "" }
    }
}
Drag options to blanks, or click blank then click option'
A.wrappedValue
B!=
C==
D.projectedValue
Attempts:
3 left
💡 Hint
Common Mistakes
Using .projectedValue instead of .wrappedValue
Using == instead of != in the filter
Forgetting to access wrappedValue