Bird
Raised Fist0
iOS Swiftmobile~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of passing data to a destination view controller in iOS Swift?
easy
A. To change the app's theme colors dynamically
B. To handle user authentication automatically
C. To improve app performance by caching data
D. To share information so the next screen can display relevant content

Solution

  1. Step 1: Understand data sharing between screens

    Passing data allows one screen to send information to another so the next screen can show content based on that data.
  2. Step 2: Identify the purpose of passing data

    This process is mainly to make the user experience relevant by showing specific information on the destination screen.
  3. Final Answer:

    To share information so the next screen can display relevant content -> Option D
  4. Quick Check:

    Passing data = share info for relevant display [OK]
Hint: Passing data means sharing info for next screen display [OK]
Common Mistakes:
  • Confusing data passing with UI styling
  • Thinking it improves performance directly
  • Assuming it handles authentication
2. Which method is commonly overridden to pass data to the destination view controller before a segue?
easy
A. prepare(for:sender:)
B. applicationDidBecomeActive()
C. viewWillAppear()
D. viewDidLoad()

Solution

  1. Step 1: Identify the method for segue preparation

    The method prepare(for:sender:) is called before a segue happens and is used to set data on the destination controller.
  2. Step 2: Confirm other methods are unrelated

    viewDidLoad() and viewWillAppear() are lifecycle methods but not used for passing data during segue. applicationDidBecomeActive() is an app lifecycle method.
  3. Final Answer:

    prepare(for:sender:) -> Option A
  4. Quick Check:

    Data passing before segue = prepare(for:sender:) [OK]
Hint: Use prepare(for:sender:) to pass data before segue [OK]
Common Mistakes:
  • Using viewDidLoad() to pass data
  • Confusing lifecycle methods with segue methods
  • Trying to pass data after segue completes
3. Given the code snippet below, what will be the value of destinationVC.userName after the segue?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  if segue.identifier == "showDetail" {
    let destinationVC = segue.destination as! DetailViewController
    destinationVC.userName = "Alice"
  }
}
medium
A. "Alice"
B. nil
C. An empty string ""
D. Causes a runtime crash

Solution

  1. Step 1: Check the segue identifier and casting

    The code checks if the segue identifier is "showDetail" and casts the destination to DetailViewController safely using forced cast.
  2. Step 2: Assign the userName property

    It sets destinationVC.userName to "Alice" before the segue completes.
  3. Final Answer:

    "Alice" -> Option A
  4. Quick Check:

    prepare sets userName = "Alice" [OK]
Hint: Check identifier and cast before assigning data [OK]
Common Mistakes:
  • Forgetting to check segue.identifier
  • Not casting destinationVC correctly
  • Assuming userName remains nil
4. What is the error in the following code that causes a crash when performing the segue?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  let destinationVC = segue.destination as! DetailViewController
  destinationVC.data = "Hello"
}
medium
A. Assigning data after the segue completes
B. Using forced cast instead of optional cast
C. Missing check for segue.identifier before casting
D. Not calling super.prepare(for:sender:)

Solution

  1. Step 1: Identify missing segue identifier check

    The code casts destinationVC without checking if the segue.identifier matches the expected one, risking wrong destination type.
  2. Step 2: Understand crash cause

    If the segue is not to DetailViewController, forced cast fails and crashes the app.
  3. Final Answer:

    Missing check for segue.identifier before casting -> Option C
  4. Quick Check:

    Always check segue.identifier before casting [OK]
Hint: Always check segue.identifier before casting destination [OK]
Common Mistakes:
  • Skipping identifier check causes crash
  • Assuming forced cast is always safe
  • Ignoring optional cast or guard statements
5. You want to pass a user's score (an Int) from GameViewController to ResultViewController using a segue with identifier "showResult". Which code snippet correctly passes the score safely?
hard
A. override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "showResult" { let dest = segue.destination as! ResultViewController dest.score = userScore } }
B. override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "showResult" { guard let dest = segue.destination as? ResultViewController else { return } dest.score = userScore } }
C. override func prepare(for segue: UIStoryboardSegue, sender: Any?) { let dest = segue.destination as? ResultViewController dest?.score = userScore }
D. override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "showResult" { let dest = segue.destination dest.score = userScore } }

Solution

  1. Step 1: Check segue identifier and safely cast destination

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "showResult" { guard let dest = segue.destination as? ResultViewController else { return } dest.score = userScore } } uses a guard let to safely cast segue.destination to ResultViewController only if the identifier matches "showResult".
  2. Step 2: Assign the score property safely

    Inside the guard, dest.score is assigned userScore safely, avoiding crashes if cast fails.
  3. Final Answer:

    Checks identifier and uses guard let for safe casting -> Option B
  4. Quick Check:

    Safe cast + identifier check = override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "showResult" { guard let dest = segue.destination as? ResultViewController else { return } dest.score = userScore } } [OK]
Hint: Use guard let with identifier check for safe data passing [OK]
Common Mistakes:
  • Forcing cast without identifier check (Option A)
  • Not checking identifier before casting (Option B)
  • Not casting destination at all (Option D)
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard