Checking whether values of unknown type are actually wrapped in Optional

Shoutout to Mike Ash’s mini protocol for checking whether the type you’re dealing with is an optional with a nil value. Handy for generic functions when you’re not sure whether you’ve been passed a straight value, or a value wrapped in an optional:

private protocol OptionalP {
    var isNil: Bool { get }

extension Optional: OptionalP {
    var isNil: Bool { return self == nil }

switch aValueOfSomeType {
case let anOptional = aValueOfSomeType as? OptionalP where anOptional.isNil: 
// ladies and gentlemen, this value is wrapped in an Optional and is nil 

This protocol is from his implementation of type safe user defaults, worth checking out in itself, obviously.

Zoë Smith @zoe