What if you could send information between screens without writing messy code every time?
Why Passing data to destination in iOS Swift? - Purpose & Use Cases
Imagine you have two screens in your app: one shows a list of friends, and the other shows details about a selected friend. You want to send the friend's name and photo from the list screen to the detail screen manually.
Doing this manually means writing lots of code to find the right screen, create it, and then copy each piece of data one by one. It's easy to forget something or make mistakes, and the code becomes messy and hard to fix.
Passing data to destination lets you send all the needed information smoothly when moving from one screen to another. It keeps your code clean and makes sure the right data arrives exactly where it should.
let detailVC = DetailViewController() detailVC.name = selectedName detailVC.photo = selectedPhoto navigationController?.pushViewController(detailVC, animated: true)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if let detailVC = segue.destination as? DetailViewController { detailVC.name = selectedName detailVC.photo = selectedPhoto } }
This makes your app flow naturally, letting users see the right information instantly when they move between screens.
When you tap a contact in your phone's address book, the app passes that contact's details to the next screen so you can see their phone number and email without delay.
Passing data to destination avoids messy manual copying.
It keeps your app's navigation smooth and reliable.
It helps users get the right info exactly when they need it.
Practice
Solution
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.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.Final Answer:
To share information so the next screen can display relevant content -> Option DQuick Check:
Passing data = share info for relevant display [OK]
- Confusing data passing with UI styling
- Thinking it improves performance directly
- Assuming it handles authentication
Solution
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.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.Final Answer:
prepare(for:sender:) -> Option AQuick Check:
Data passing before segue = prepare(for:sender:) [OK]
- Using viewDidLoad() to pass data
- Confusing lifecycle methods with segue methods
- Trying to pass data after segue completes
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"
}
}Solution
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.Step 2: Assign the userName property
It sets destinationVC.userName to "Alice" before the segue completes.Final Answer:
"Alice" -> Option AQuick Check:
prepare sets userName = "Alice" [OK]
- Forgetting to check segue.identifier
- Not casting destinationVC correctly
- Assuming userName remains nil
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! DetailViewController
destinationVC.data = "Hello"
}Solution
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.Step 2: Understand crash cause
If the segue is not to DetailViewController, forced cast fails and crashes the app.Final Answer:
Missing check for segue.identifier before casting -> Option CQuick Check:
Always check segue.identifier before casting [OK]
- Skipping identifier check causes crash
- Assuming forced cast is always safe
- Ignoring optional cast or guard statements
GameViewController to ResultViewController using a segue with identifier "showResult". Which code snippet correctly passes the score safely?Solution
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".Step 2: Assign the score property safely
Inside the guard, dest.score is assigned userScore safely, avoiding crashes if cast fails.Final Answer:
Checks identifier and uses guard let for safe casting -> Option BQuick 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]
- Forcing cast without identifier check (Option A)
- Not checking identifier before casting (Option B)
- Not casting destination at all (Option D)
Ready to prove it?
Pass the test and this pattern moves unknown
