Complete the code to unwrap the optional value using guard let.
func printName(name: String?) {
guard [1] = name else {
print("No name provided.")
return
}
print("Name is \(unwrappedName)")
}Using guard let unwrappedName = name unwraps the optional name safely for use inside the function.
Complete the code to unwrap the optional and print the unwrapped value.
func greet(user: String?) {
guard let name = [1] else {
print("User not found.")
return
}
print("Hello, \(name)!")
}The optional user is unwrapped using guard let name = user to safely access its value.
Fix the error in the guard statement to unwrap the optional correctly.
func checkAge(age: Int?) {
guard let age = [1] else {
print("Age is missing.")
return
}
print("Age is \(age)")
}The optional parameter age must be unwrapped by using its own name in the guard statement.
Fill both blanks to unwrap the optional and check if the unwrapped value is greater than 18.
func verify(age: Int?) {
guard [1] = age, [2] > 18 else {
print("Access denied.")
return
}
print("Access granted.")
}First, unwrap the optional with let unwrappedAge = age. Then check if unwrappedAge > 18 to allow access.
Fill all three blanks to unwrap two optionals and check if the unwrapped user is a non-empty string.
func validate(user: String?, password: String?) {
guard [1] = user, [2] = password, ![3].isEmpty else {
print("Invalid input.")
return
}
print("User and password are valid.")
}Unwrap user and password with let unwrappedUser = user and let unwrappedPassword = password. Then check if unwrappedUser is not empty.