Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to save data locally using UserDefaults.
iOS Swift
UserDefaults.standard.set([1], forKey: "username")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the key name instead of the value
Using the UserDefaults object instead of a value
✗ Incorrect
We save the string "JohnDoe" locally with the key "username" to access it offline.
2fill in blank
mediumComplete the code to retrieve a locally saved string from UserDefaults.
iOS Swift
let name = UserDefaults.standard.string(forKey: [1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name instead of the key string
Using a wrong key string
✗ Incorrect
We use the same key "username" to get the saved string from local storage.
3fill in blank
hardFix the error in the code to save an integer locally.
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 'setInteger' instead of 'set'
Using save or store which don't exist
✗ Incorrect
The correct method to save any type is 'set'.
4fill in blank
hardFill both blanks to check if local data exists and print it.
iOS Swift
if let name = UserDefaults.standard.[1](forKey: [2]) { print(name) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong key strings
Using wrong method names
✗ Incorrect
We use 'string' to get the string value and the key must be "username" to match saved data.
5fill in blank
hardFill all three blanks to save, retrieve, and print a boolean value locally.
iOS Swift
UserDefaults.standard.[1](true, forKey: [2]) let isLoggedIn = UserDefaults.standard.[3](forKey: "loggedIn") print(isLoggedIn)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' to retrieve boolean
Using wrong keys
✗ Incorrect
We save with 'set', use the key "loggedIn", and retrieve with 'bool' method for boolean values.