Bird
0
0

Consider this Swift code:

hard📝 Application Q9 of 15
iOS Swift - Swift Language Essentials
Consider this Swift code:
func fetchData() throws -> String {
  throw NSError(domain: "Error", code: 1)
}

func process() throws {
  try fetchData()
}

try? process()

What happens if process() is called with try??
AReturns the string from fetchData
BReturns nil because fetchData throws an error
CThrows a runtime error
DCompilation error due to missing catch
Step-by-Step Solution
Solution:
  1. Step 1: Analyze fetchData throwing behavior

    fetchData() always throws an error.
  2. Step 2: Understand try? with throwing functions

    try? process() returns nil if an error is thrown anywhere inside.
  3. Final Answer:

    Returns nil because fetchData throws an error -> Option B
  4. Quick Check:

    try? returns nil on error = C [OK]
Quick Trick: try? returns nil if any called function throws [OK]
Common Mistakes:
  • Expecting runtime error
  • Expecting string return
  • Confusing try? with try!

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes