Daily iOS
Swift

Extending Optional

Extend Optional so that any optional can be asked for its nil status, like someOptional.isNil. How would you implement it?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

The standard Optional is an enum with none and some cases, so add an isNil computed property in extension Optional that returns true when self matches .none and false otherwise. Since it's pattern matching, it works on any optional with no Equatable or other constraint on Wrapped.

Share a question