[swift-users] still wrestling with a type conversion problem

David Baraff davidbaraff at gmail.com
Wed Jul 5 10:59:53 CDT 2017


> func decodeMe<T>(_ payload:Any) -> T? {
>     if  let dt = T.self as? DecodableFromAny {
>         return type(of: dt).fromAny(payload) as! T
>     } else {
>         return payload as? T
>     }
> }


Auggh! I only thought it worked.  Luckily, I had help.  My 16 year-old son looked at this code, and *he* figured it out:  I meant to write
   
>     if  let dt = T.self as? DecodableFromAny {

as

     if  let dt = T.self as? DecodableFromAny.Type {



Now, I swear, it really does work.



More information about the swift-users mailing list