Bird
0
0

Identify the issue in this Swift code intended to detect an Apple Watch:

medium📝 Debug Q7 of 15
iOS Swift - iOS Basics and Setup
Identify the issue in this Swift code intended to detect an Apple Watch:
let device = WKInterfaceDevice.current()
if device.systemName == "watchOS" {
    print("Apple Watch detected")
}
AsystemName does not return "watchOS" on Apple Watch
BWKInterfaceDevice.current() is invalid on Apple Watch
CMissing else clause causes runtime error
DUsing == instead of === for string comparison
Step-by-Step Solution
Solution:
  1. Step 1: Understand systemName property

    The systemName property on WKInterfaceDevice returns "watchOS" on Apple Watch, but in practice it may not be reliable or may differ.
  2. Step 2: Common issue

    Developers often find systemName returns "watchOS" only on simulator or may not be consistent.
  3. Step 3: Better approach

    Use systemVersion or other reliable methods to detect Apple Watch.
  4. Final Answer:

    systemName does not return "watchOS" on Apple Watch -> Option A
  5. Quick Check:

    Verify systemName values on real devices [OK]
Quick Trick: systemName may not reliably equal 'watchOS' on Apple Watch [OK]
Common Mistakes:
  • Assuming systemName always returns 'watchOS'
  • Confusing string equality operators
  • Ignoring device API limitations

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes