I've written an extension that searches a Collection
for an object of a certain type.
extension Collection {
/// Finds and returns the first element matching the specified type or nil.
func findType<T>(_ type: T.Type) -> Iterator.Element? {
if let index = (index { (element: Iterator.Element) in
String(describing: type(of: element)) == String(describing: type) }) {
return self[index]
}
return nil
}
}
Now in Xcode 9 / Swift 4, the snippet type(of: element))
is underlined with error
Non-nominal type 'T' does not support explicit initialization
The error is strange because I'm not initializing an object.
This answer https://stackoverflow.com/a/46114847/2854041 suggests that perhaps it's a type issue - did the String(describing:) initializer change in Swift 4?