0
0
iOS Swiftmobile~20 mins

Passing data to destination in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Data Passing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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"
  }
}
A"Hello Swift"
Bnil
C"showDetail"
DCrash due to wrong cast
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.
navigation
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")
  }
}
ANo data
Bnil
CSwift Rocks
DCrash due to missing storyboard
Attempts:
2 left
💡 Hint
Check when viewDidLoad is called and if the data property is set before that.
lifecycle
advanced
2: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")
  }
}
ABecause prepare(for:sender:) is called after viewDidLoad
BBecause the data is set asynchronously after viewDidLoad runs
CBecause receivedText is private and inaccessible
DBecause the segue identifier is missing
Attempts:
2 left
💡 Hint
Think about when the DispatchQueue.main.async block executes relative to viewDidLoad.
📝 Syntax
advanced
2: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
  }
}
Adest.message := "Hello from source"
Bdest.message == "Hello from source"
Cdest.message <- "Hello from source"
Ddest.message = "Hello from source"
Attempts:
2 left
💡 Hint
Remember the assignment operator in Swift.
🔧 Debug
expert
3: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
  }
}
AThe label outlet is not connected in storyboard, so label is nil
BreceivedText is nil because prepare(for:) is not called
CviewDidLoad runs before prepare(for:), so label.text is empty
DThe segue identifier is incorrect, so prepare(for:) is skipped
Attempts:
2 left
💡 Hint
Check if the label outlet is properly connected in Interface Builder.