0
0
iOS Swiftmobile~10 mins

Why local data enables offline functionality in iOS Swift - Test Your Understanding

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

Complete 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'
Aset
Busername
CUserDefaults
D"JohnDoe"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the key name instead of the value
Using the UserDefaults object instead of a value
2fill in blank
medium

Complete 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'
A"username"
Bname
CUserDefaults
D"user"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name instead of the key string
Using a wrong key string
3fill in blank
hard

Fix 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'
Aset
BsetInteger
Csave
Dstore
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'setInteger' instead of 'set'
Using save or store which don't exist
4fill in blank
hard

Fill 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'
Astring
B"username"
C"user"
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong key strings
Using wrong method names
5fill in blank
hard

Fill 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'
Aset
B"loggedIn"
Cbool
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' to retrieve boolean
Using wrong keys