Challenge - 5 Problems
Data Passing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
Passing data using prepare(for:sender:)
What will be the value of
destinationVC.receivedText after this segue is performed?iOS Swift
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "showDetail" { let destinationVC = segue.destination as! DetailViewController destinationVC.receivedText = "Hello Swift" } }
Attempts:
2 left
💡 Hint
Remember that prepare(for:sender:) is called before the segue happens and you can set properties on the destination view controller.
✗ Incorrect
The code safely casts the destination view controller and sets its receivedText property to "Hello Swift" before the segue happens, so the destinationVC.receivedText will be "Hello Swift".
intermediate
2:00remaining
Passing data with programmatic navigation
What will be printed when the button is tapped?
iOS Swift
class FirstViewController: UIViewController { @IBAction func buttonTapped(_ sender: UIButton) { let secondVC = SecondViewController() secondVC.data = "Swift Rocks" navigationController?.pushViewController(secondVC, animated: true) } } class SecondViewController: UIViewController { var data: String? override func viewDidLoad() { super.viewDidLoad() print(data ?? "No data") } }
Attempts:
2 left
💡 Hint
Check when viewDidLoad is called and if the data property is set before that.
✗ Incorrect
The data property is set before pushing the view controller, so when viewDidLoad runs, data contains "Swift Rocks" and it prints that.
❓ lifecycle
advanced2:00remaining
Data availability timing in destination VC
Why might
receivedText be nil inside viewDidLoad() of the destination view controller when using a segue?iOS Swift
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { let destinationVC = segue.destination as! DetailViewController DispatchQueue.main.async { destinationVC.receivedText = "Delayed Data" } } class DetailViewController: UIViewController { var receivedText: String? override func viewDidLoad() { super.viewDidLoad() print(receivedText ?? "No data") } }
Attempts:
2 left
💡 Hint
Think about when the DispatchQueue.main.async block executes relative to viewDidLoad.
✗ Incorrect
The data is assigned asynchronously on the main queue, which happens after viewDidLoad runs, so receivedText is still nil during viewDidLoad.
📝 Syntax
advanced2:00remaining
Correct syntax for passing data with segue
Which option correctly passes a string to the destination view controller's property
message in prepare(for:sender:)?iOS Swift
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "showMessage" { let dest = segue.destination as! MessageViewController // Pass data here } }
Attempts:
2 left
💡 Hint
Remember the assignment operator in Swift.
✗ Incorrect
Option D uses the correct assignment operator '=' to set the property. The others use invalid operators causing syntax errors.
🔧 Debug
expert3:00remaining
Why does data not appear in destination VC?
Given this code, why does the destination view controller's label show empty text after navigation?
iOS Swift
class FirstVC: UIViewController { var textToSend = "Important Data" override func prepare(for segue: UIStoryboardSegue, sender: Any?) { let dest = segue.destination as! SecondVC dest.receivedText = textToSend } } class SecondVC: UIViewController { var receivedText: String? @IBOutlet weak var label: UILabel! override func viewDidLoad() { super.viewDidLoad() label.text = receivedText } }
Attempts:
2 left
💡 Hint
Check if the label outlet is properly connected in Interface Builder.
✗ Incorrect
If the label outlet is not connected, label is nil and setting label.text does nothing, so the label appears empty.