swiftlang/swift
GitHub で見る[SR-4451] type(of:) behaviour change from Swift 3.0.2 to Swift 3.1.
Open
#47,028 opened on 2017年3月31日
bugcompilergood first issuetype checker
説明
| Previous ID | SR-4451 |
| Radar | rdar://problem/31404284 |
| Original Reporter | razvandub (JIRA User) |
| Type | Bug |
Attachment: Download
| Votes | 0 |
| Component/s | Compiler |
| Labels | Bug, StarterBug, TypeChecker |
| Assignee | None |
| Priority | Medium |
md5: 59a922e46e7486ff5d399c2af0e0d2d8
Issue Description:
It seems that the behaviour of "type(of: )" has changed from Swift 3.0.2 to Swift 3.1 w.r.t. optional Any? closure parameters referenced as Any.
"type(of: oItem) as? T.Type" below returns "__NSCFString.Type" when using Swift 3.0.2 compiler and "nil" when using Swift 3.1 compiler.
import Foundation
internal extension Set where Element: NSObject {
internal func items <T: NSObject> (ofType itemType: T.Type) -> Set<T>? {
return NSSet(set: self).filtered(using: NSPredicate { (oItem: Any /* Note Any instead of Any? */, _) -> Bool in
return nil != (type(of: oItem) as? T.Type) // Swift 3.0.2 => true, Swift 3.1 => false.
// return oItem is T // Swift 3.0.2 => true, Swift 3.1 => true.
}) as? Set<T>
}
}
let stringsOfTypeNSObject = (["1", "2"] as Set<NSString>).items(ofType: NSObject.self)
print(stringsOfTypeNSObject ?? []) // Swift 3.0.2 => [1, 2], Swift 3.1 => [].