0
0
iOS Swiftmobile~10 mins

UserDefaults for simple values in iOS 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 save a string value to UserDefaults.

iOS Swift
UserDefaults.standard.set("Hello", forKey: [1])
Drag options to blanks, or click blank then click option'
A"save"
B"greeting"
C"key"
D"value"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a key that is not a string.
Forgetting to put quotes around the key.
2fill in blank
medium

Complete 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'
A"text"
B"message"
C"greeting"
D"note"
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.
3fill in blank
hard

Fix 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'
Aset
Bstore
Cput
Dsave
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names like 'save' or 'put'.
Forgetting to use the correct method causes runtime errors.
4fill in blank
hard

Fill 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'
Aset
Bbool
Cget
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' instead of 'bool' to retrieve a boolean.
Using wrong method names causing errors.
5fill in blank
hard

Fill 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'
Aset
Bdouble
C"price"
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'float' instead of 'double' to retrieve a double value.
Using different keys for saving and retrieving.