         for result in checkingResults {
 
             switch result.resultType {
-            case NSTextCheckingResult.CheckingType.address:
+            case .address:
                 var ranges = rangesForDetectors[.address] ?? []
-                let tuple = (result.range, result.addressComponents) as (NSRange, Any?)
+                let tuple: (NSRange, MessageTextCheckingType) = (result.range, .addressComponents(result.addressComponents))
                 ranges.append(tuple)
                 rangesForDetectors.updateValue(ranges, forKey: .address)
-            case NSTextCheckingResult.CheckingType.date:
+            case .date:
                 var ranges = rangesForDetectors[.date] ?? []
-                let tuple = (result.range, result.date) as (NSRange, Any?)
+                let tuple: (NSRange, MessageTextCheckingType) = (result.range, .date(result.date))
                 ranges.append(tuple)
                 rangesForDetectors.updateValue(ranges, forKey: .date)
-            case NSTextCheckingResult.CheckingType.phoneNumber:
+            case .phoneNumber:
                 var ranges = rangesForDetectors[.phoneNumber] ?? []
-                let tuple = (result.range, result.phoneNumber) as (NSRange, Any?)
+                let tuple: (NSRange, MessageTextCheckingType) = (result.range, .phoneNumber(result.phoneNumber))
                 ranges.append(tuple)
                 rangesForDetectors.updateValue(ranges, forKey: .phoneNumber)
-            case NSTextCheckingResult.CheckingType.link:
+            case .link:
                 var ranges = rangesForDetectors[.url] ?? []
-                let tuple = (result.range, result.url) as (NSRange, Any?)
+                let tuple: (NSRange, MessageTextCheckingType) = (result.range, .link(result.url))
                 ranges.append(tuple)
                 rangesForDetectors.updateValue(ranges, forKey: .url)
             default:


    private func detectorAttributes(for checkingResultType: NSTextCheckingResult.CheckingType) -> [NSAttributedStringKey: Any] {
        switch checkingResultType {
        case NSTextCheckingResult.CheckingType.address:
            return addressAttributes
        case NSTextCheckingResult.CheckingType.date:
            return dateAttributes
        case NSTextCheckingResult.CheckingType.phoneNumber:
            return phoneNumberAttributes
        case NSTextCheckingResult.CheckingType.link:
            return urlAttributes
        default:
            fatalError("Received an unrecognized NSTextCheckingResult.CheckingType")

    }
    
}

fileprivate enum MessageTextCheckingType {
    case addressComponents([NSTextCheckingKey: String]?)
    case date(Date?)
    case phoneNumber(String?)
    case link(URL?)
}
