Monday, April 14, 2014

JLoS for Objective-C

Over the past few years, I’ve been getting back into coding, starting with App Engine  and then javascript, and onto phonegap and then some native app code. The one thing that I’ve always felt very uncomfortable with is sql and all formal data structures (such as "core data” in x-code.)
So a couple of years ago, I came up with a very simple javascript object I called “JLoS” - which stands for JSON for Local Storage. It’s just a few lines of code, but I found it very useful for phonegap and web development. Basically, it’s just a json object, which you initialise with a file name and save it to local storage…(Posted originally on pastebin  and more recently on github
It’s just so much easier to work with JSON than formal data structures, whether on the server side (ie MongoDB) or locally in javascript or native apps. I have no idea what other people do to manage simple data structures, specially on a browser based app where the options seem to be hovering in no man’s land.
There is less reason to use jlos on native apps, but if you are downloading and uploading json in any case, why would you want to start converting it to fancy Core Data structures? 
So last weekend, I created JLoS for objective c… and put it on github
Not that any one would notice ;), but the concept is similar to JLoS for javascript, except that it is just an “nsdictionary” saved to file... Or you can think of it as and extended and extensible NSUserDefaults. It is also similar to “Core Data” which, to my surprise has some of the quirks of jlos. (eg Since you can create multiple instances of the object from the same file, you have to make sure you manage reading, saving, and refreshing its content and to coordinate updates among the different instances.)
In some places, the formalism of core data obviously serves well, but often you just want to store loose data structures in files and that’s what JLoS does… and objective c is very good to at transforming between json and nsdictionary so it's easily exportable too.
Hopefully some people will find it useful.