How can you check if a type is Optional in Swift?

How can you check if a type is Optional in Swift?

Let's say I have a variable of type PartialKeyPath, where:

struct Foo {
    let bar: String
    let baz: String?
}

typealias Property<Root> = (key: PartialKeyPath<Root>, value: Any?)
typealias Properties<Root> = [Property<Root>]

Now let's say that I repeat the property instance:

properties.forEach { prop in
    let valueType1 = type(of: prop.key).valueType
    let valueType2 = type(of: value)

    ...

How can I check here if valueType1 is Optional<valueType2>, or is it optional for any other flavor?

So far the only way to find me is really ugly ...

+4
source share
4 answers

This is a hacky but working solution:

func isOptional(_ type: Any.Type) -> Bool {
    let typeName = String(describing: type)
    return typeName.hasPrefix("Optional<")
}

Test:

let t1 = Int?.self
let t2 = Bool.self

print(isOptional(t1))
// true

print(isOptional(t2))
// false
+2
source

Using a similar approach to the Optional field type does not match the protocol in Swift 3 , you can define a "dummy protocol" for Optionaland use it to get the wrapped metatype:

protocol OptionalProtocol {
  // the metatype value for the wrapped type.
  static var wrappedType: Any.Type { get }
}

extension Optional : OptionalProtocol {
  static var wrappedType: Any.Type { return Wrapped.self }
}

, :

func isOptionalType(_ type: Any.Type) -> Bool {
  return type is OptionalProtocol.Type
}

print(isOptionalType(String.self)) // false
print(isOptionalType(String?.self)) // true

, " " :

struct Foo {
  let bar: String
  let baz: String?
}

struct Property<Root> {
  var key: PartialKeyPath<Root>
  var value: Any
}

let properties = [Property(key: \Foo.baz, value: "hello")]

/// Attempt to get the `Wrapped` metatype from a metatype of an
/// `Optional<Wrapped>`. If not an `Optional`, will return `nil`.
func wrappedTypeFromOptionalType(_ type: Any.Type) -> Any.Type? {
  return (type as? OptionalProtocol.Type)?.wrappedType
}

for property in properties {
  let valueType1 = type(of: property.key).valueType
  let valueType2 = type(of: property.value)

  if wrappedTypeFromOptionalType(valueType1) == valueType2 {
    print("\(valueType1) == Optional<\(valueType2)>")
  }
}

// Optional<String> == Optional<String>

, , .

+2

Is it possible to use a mirror reflecting Any and check displayStyle is optional?

func isOptional(any:Any) -> Bool {

  let mirror = Mirror(reflecting: any)
  if mirror.displayStyle == .Optional {
      return true
  } else {
      return false
  }

}

More on mirroring style: https://developer.apple.com/documentation/swift/mirror.displaystyle

+1
source

Reply to @kelins:

postfix operator ...?!
postfix func ...?!<T>(_ instance: T) -> Bool {
    let subject = "\(Mirror(reflecting: instance).subjectType)"
    return !subject.hasPrefix("Optional") 
}

And in the spirit of @ Ercell0s, the answer is this excellent method:

func isOptional<T>(_ instance: T) -> Bool {
    guard let displayStyle = Mirror(reflecting: instance).displayStyle 
        else { return false }
    return displayStyle == .optional
}
0
source

Source: https://habr.com/ru/post/1690368/


All Articles