Skip to main content

Posts

Showing posts from March, 2015

Creating better JSON: An EPUB odyssey

The dos and don'ts of keeping it clean I'm going to begin this post with a list of dos and don'ts: don't nest content more than necessary use a limited number of keywords don't mix types unnecessarily don't write JSON that will take a lot of deciphering and decoding keep things as flat and simple as possible make the intention of the data as clear as possible think about how the JSON will most likely be interpreted without being restrictive or prescriptive use common concepts to indicate elements that are not straightforward values remember that arrays indicate order and dictionaries (or objects) are for unordered key/value access of information Example structure Now for a JSON structure I created to represent the internal structure of an EPUB following the dos and don'ts listed above: { "platform": "epub", "version": 3, "text-encoding": "UTF-8", "subdirectories":

Fitness Apps: Week 10 (Google Fit, MyFitnessPal and MapMyRun)

The sound of a pneumatic woodpecker, the smell of marijuana as I round a corner, a loved up couple sharing earphones. It reminds me of an earlier part of my life where I'd sleep on floors at the end of a party and rise before everyone else excited to explore the surrounding area. Early mornings are a place of patience, of slow pace, of low stress. Where newspapers are bought and dogs are walked. A world through which I've grown used to happily sleeping, but a place to which I've now returned to enjoy the mist and dew. But this time it's different. It's regular, it's for a purpose. Last week I ran 9.95 km one morning before breakfast. In the process I was overtaken by two other runners, but it didn't matter because the important thing was distance and height gain. After three months of running, I can't expect to be the fastest but I can enjoy pushing myself further and further. Far beyond what I expected when I began. Although, I do wish I could wear

Fitness Apps: Week 9 (Google Fit, MyFitnessPal and MapMyRun)

Keeping it brief This is a brief update as I approach week 9. I'm still using MyFitnessPal every day but my interest has shifted to MapMyRun  as I begin to challenge myself to run further and faster. Today was the first day where I ran 8.3 km (or just over 5 miles in old money) before breakfast. It was also the first day where a man stood still, saluted and made the sign of the cross, as if to say he thought I was going to peg it. Resisting the urge to tell him he was a cheeky sod, I ran on at twice the speed. What's next? Yesterday I ran 7 km, the day before 6.3 km, and today 8.3 km (which according to MapMyRun places me in the top 25% for distance). I'm no longer worried about extending the length of the run and I'm tempted to make tomorrow the day I run 10 km. The only constraint is time, and this is the thing which dictates what's possible. Looking ahead even further As I improve, a voice in my head starts asking if I could run the length of a half

Type Safety in Swift: Enums with Associated Values (Updated)

Following my first post on Type Safety in Swift , I was asked (by  @DeFrenz ) why I hadn't used enums (or an Either type). It was a question that made me think again about the employment of enums in the various JSON parsing libraries that have been released for Swift, and to look again at Apple's documentation for enumerations, and for those employing associated values in particular. This was a useful exercise because it took me back to the real issue of dealing with AnyObject or NSObject dictionaries and arrays (which is one misalignment with a strongly typed language such as Swift) and the need to take a value and transform it into one of two or more types. Individually wrapped The main difference between the approach to type safety that I took in the previous two posts on type safety is that, with the use of enums, individual values can be wrapped rather than relying on division into multiple dictionaries and arrays. Starting with the enum The starting point for t

Type Safety in Swift: A Route Back

Following  yesterday's post on Type Safety in Swift , I'd like to think about how we can meet the demands of programmers wishing to build a type to store (and amend) data that has specific keys and values, and which is received in a dictionary of mixed types; while at the same time embracing type safety and providing a means by which to enable the return of data in exactly the same format as it was received (with changes maintained). A tall order? Not really. Starting with a wrapper Here's a resurrection of the type used in yesterday's post, but with the type name changed from TwinTypes to PersonData: struct PersonData { private var stringDict = Dictionary<String,String>() private var numDict = Dictionary<String,NSNumber>() var dictionary:Dictionary<String,NSObject> { var dictionary = Dictionary<String,NSObject>() for (k,v) in stringDict { dictionary[k] = v as NSObject } for (

Type Safety in Swift: Opportunities for Control

"Swift is a type safe language. Swift helps you to be clear about the types of values your code can work with. If part of your code expects a String, type safety prevents you from passing it an Int by mistake. This restriction enables you to catch and fix errors as early as possible in the development process." ( Apple Developer )  The problem In the following example the type Dictionary<String, NSObject> is inferred and this allows us an incredible amount of freedom but it is not type safe. var dict = ["one":1,"two":"two","three":3,"four":4] let cc = dict["one"] // 1 dict["three"] = "three" The reason it is not type safe is because we can change values however we like as long as ultimately the value's type can be cast to a foundation object. This means that we never know the type that any given key is going to return, which in turn means the compiler cannot help us with rega

Fitness Apps: Week 8 (Google Fit, MyFitnessPal and MapMyRun)

Over a quarter of a century ago, a well-known sports brand began using the tagline "Just do it!" , but nowadays the message has been buried beneath the message to buy the right shoes for your activity (and foot arch), make sure you are in your burn zone using a heart-rate monitoring watch, use apps to motivate yourself, eat the right food, push the right weights, don't become skinny fat , and so on and so on. But while all of these things encourage a healthy approach and enhance exercise, at the same time they are barriers to getting out there and doing it. All the same, I see a good number of runners and cyclists out and about in the city where I live, not to mention early-morning dog walkers (sometimes running with their dogs). So people are out there doing it. Sometimes with music players or phones strapped to their arms, and maybe other tech, I don't know, but the important thing is they are being active. Has the modern world of apps contributed to this? Well