Last active
September 19, 2021 01:18
-
-
Save paulw11/cd7540cc5f2185ee9d58ce003469a6d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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