Skip to content

Instantly share code, notes, and snippets.

@paulw11
Last active September 19, 2021 01:18
Show Gist options
  • Save paulw11/cd7540cc5f2185ee9d58ce003469a6d4 to your computer and use it in GitHub Desktop.
Save paulw11/cd7540cc5f2185ee9d58ce003469a6d4 to your computer and use it in GitHub Desktop.
import Foundation
class BaseClass {}
class A : BaseClass {
}
class B : BaseClass {
}
var refA : A? = A()
var refB : B? = B()
var refArray = [BaseClass?]()
refArray.append(refA)
refArray.append(refB)
func findMatch<T:BaseClass>( in array:[BaseClass?]) -> T? {
let element = array.first { element in
element is T
}
return element as? T
}
var someRefA: A?
someRefA = findMatch(in: refArray)
print(someRefA)
import Foundation
class BaseClass {}
class A : BaseClass {
}
class B : BaseClass {
}
protocol OptionalProtocol {
// the metatype value for the wrapped type.
var wrappedType: Any.Type { get }
}
extension Optional : OptionalProtocol {
var wrappedType: Any.Type { return Wrapped.self
}
}
var refA : A?
var refB : B?
var refArray = [BaseClass?]()
refArray.append(refA)
refArray.append(refB)
for aThing in refArray {
print(aThing.wrappedType)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment