Is it possible to check if a variable is optional, and what type does it wrap?
You can check if a variable is a specific option:
let someString: String? = "oneString" var anyThing: Any = someString anyThing.dynamicType // Swift.Optional<Swift.String> anyThing.dynamicType is Optional<String>.Type // true anyThing.dynamicType is Optional<UIView>.Type // false
But is it possible to repeat the verification of any type of optional? Something like:
anyThing.dynamicType is Optional.Type // fails since T cant be inferred // or anyThing.dynamicType is Optional<Any>.Type // false
And as soon as you find out that you have an option, extract the type that it wraps:
swift
LopSae Sep 18 '15 at 6:50 2015-09-18 06:50
source share