I find myself writing code like this when handling JSON using Codable, because of the instances where the value of the same key might be of variable types: init(from decoder: Decoder) throws { do { let anim = try AppearAnimation(from: decoder) self = ComponentAnimation.appear(anim) } catch DecodingError.typeMismatch(_, _) { } catch { throw error } do { let anim = try FadeAnimation(from: decoder) self = ComponentAnimation.fade_in(anim) } catch DecodingError.typeMismatch(_, _) { } catch { throw error } do { let anim = try MoveAnimation(from: decoder) self = ComponentAnimation.move_in(anim) } catch DecodingError.typeMismatch(_, _) { } catch { throw error } do { let anim = try ScaleFadeAnimation(from: decoder) self = ComponentAnimation.scale_fade(anim) } catch {
Technology tips for writers, editors, designers, illustrators, programmers and publishers, and now Retro Gamers.