Skip to main content


Showing posts from August, 2018

Swift: Why try? isn't good enough for JSON

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 {

Swift: Codable Encounters of the Enum Kind

In my everyday life I am a publisher working with tools like InDesign but also performing web development and in part experimenting and developing on iOS and Android. Working with JSON is a passion of mine, but it's not one that I get to indulge full time. So I spend long periods of time away from Xcode and Swift during which I easily forget all the stuff I learnt about Codable aside from the basics. But recently I returned to my JSON work that I started before the Codable protocol was added to Swift and started stripping out my own bespoke JSON approach and replacing it with Codable elements. One question I had was regarding the behaviour in the situation where you have a codable enum with a raw type of String. What would happen if that enum was inserted as the type of a property where a string would be present in the JSON. My hope was that if the string received matched the raw type of one of the enums then all would work well. Otherwise there would be more complicated w

Local Storage: How to debug it and why Safari throws errors

If you are testing localStorage with files on your machine, you will find that Chrome and Firefox have no problem with this and work normally but Safari refuses to store any key/value pairs when the page is not delivered via a server. Safari throws the error "SecurityError: The operation is insecure." inside the Debugger (Develop > Show Web Inspector > Debugger) and the Local Storage remains empty (Develop > Show Web Inspector > Storage > Local Storage). The solution is to either serve remotely or to use MAMP , for example, rather than attempting to load the html directly from your machine. Alternatively, use Chrome and Firefox to test locally. You can also Disable Local File Restrictions in the Safari Develop menu as a solution. Did they remove the "disable local restrictions" or something like this in the dev menu? — Jiminy Panoz (@JiminyPan) August 11, 2018 Thanks to Jiminy Panoz for the reminder on this last point and his recommend