Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to save a string value to UserDefaults.
iOS Swift
UserDefaults.standard.set("Hello", forKey: [1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a key that is not a string.
Forgetting to put quotes around the key.
✗ Incorrect
The key "greeting" is used to store the string "Hello" in UserDefaults.
2fill in blank
mediumComplete the code to retrieve a string value from UserDefaults.
iOS Swift
let message = UserDefaults.standard.string(forKey: [1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different key than the one used to save the value.
Forgetting to use quotes around the key.
✗ Incorrect
The key "greeting" matches the key used to save the string, so it retrieves the correct value.
3fill in blank
hardFix the error in the code to save an integer value to UserDefaults.
iOS Swift
UserDefaults.standard.[1](25, forKey: "age")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names like 'save' or 'put'.
Forgetting to use the correct method causes runtime errors.
✗ Incorrect
The correct method to save a value in UserDefaults is 'set'.
4fill in blank
hardFill both blanks to save and retrieve a boolean value using UserDefaults.
iOS Swift
UserDefaults.standard.[1](true, forKey: "isLoggedIn") let loggedIn = UserDefaults.standard.[2](forKey: "isLoggedIn")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' instead of 'bool' to retrieve a boolean.
Using wrong method names causing errors.
✗ Incorrect
Use 'set' to save the boolean and 'bool(forKey:)' to retrieve it from UserDefaults.
5fill in blank
hardFill all three blanks to save a double value and then retrieve it from UserDefaults.
iOS Swift
UserDefaults.standard.[1](9.99, forKey: "price") let price = UserDefaults.standard.[2](forKey: [3])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'float' instead of 'double' to retrieve a double value.
Using different keys for saving and retrieving.
✗ Incorrect
Use 'set' to save the double, 'double' to retrieve it, and the key must be "price".