0
0
iOS Swiftmobile~10 mins

Preference keys 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 using the key "username".

iOS Swift
UserDefaults.standard.set("John", forKey: [1])
Drag options to blanks, or click blank then click option'
A"username"
B"user_name"
C"user"
D"name"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a key with underscores instead of the exact key.
Forgetting to put the key in quotes.
2fill in blank
medium

Complete the code to retrieve a boolean preference with key "isDarkMode" from UserDefaults.

iOS Swift
let darkModeEnabled = UserDefaults.standard.[1]("isDarkMode")
Drag options to blanks, or click blank then click option'
Ainteger(forKey:)
Bbool(forKey:)
Cstring(forKey:)
Ddouble(forKey:)
Attempts:
3 left
💡 Hint
Common Mistakes
Using string(forKey:) when expecting a boolean.
Using integer(forKey:) or double(forKey:) incorrectly.
3fill in blank
hard

Fix the error in the code to remove a preference with key "userAge" from UserDefaults.

iOS Swift
UserDefaults.standard.[1](forKey: "userAge")
Drag options to blanks, or click blank then click option'
AremoveObject
BdeleteObject
CclearObject
DunsetObject
Attempts:
3 left
💡 Hint
Common Mistakes
Using deleteObject or clearObject which do not exist.
Using set(nil, forKey:) which is not recommended.
4fill in blank
hard

Fill both blanks to save an integer preference "launchCount" incremented by 1.

iOS Swift
let count = UserDefaults.standard.[1]("launchCount")
UserDefaults.standard.set(count [2] 1, forKey: "launchCount")
Drag options to blanks, or click blank then click option'
Ainteger(forKey:)
B+
C-
Ddouble(forKey:)
Attempts:
3 left
💡 Hint
Common Mistakes
Using double(forKey:) instead of integer(forKey:).
Using - instead of + to increment.
5fill in blank
hard

Fill all three blanks to create a dictionary preference with keys "theme", "fontSize", and "notifications".

iOS Swift
let settings: [String: Any] = ["theme": [1], "fontSize": [2], "notifications": [3]]
UserDefaults.standard.set(settings, forKey: "userSettings")
Drag options to blanks, or click blank then click option'
A"dark"
B14
Ctrue
D"light"
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes incorrectly around numbers or booleans.
Mixing up the order of values.