Picture of Michael Lee
:wave: Hey hey, I'm Michael Lee and this is my site about being a developer, being a dad and making side projects.

A Simple Dasherize Method in Objective-C

Written on June 26, 2014

I’ve recently been diving into iOS programming and decided to make a simple directory app of people I work with. The app parses a locally stored JSON file that has the name and role of a person. I’m also hosting some profile photos of the people in a Dropbox folder.

Instead of adding another key/value pair to my JSON file for every person, I wanted to just use their name to grab their corresponding profile photo from Dropbox.

Each photo is stored on Dropbox with a filename syntax of firstname-lastname.jpg. The syntax of each name in the JSON file is FirstName LastName.

I don’t think Objective-C has computed values like Ember.js does. So I created a simple dasherize method that returns a string in the format that I need for the Dropbox photos.

First, the method creates a lowercase copy of the string you pass to the method. Then each occurrence of a space is replaced with a -. Finally a dasherized string is returned, which I can stick in a formatted string to pull the photo from Dropbox.

The method isn’t as robust as Ember.js’ dasherize method, so I can definitely improve upon it to handle the same inputs Ember.js’ method does.

Thanks for taking the time to read this article. I'd love to stay in touch and share more tips on programming and design and side projects. Sign up and I'll send you my articles straight to your email.
Previous post: Ribbon
Next post: Sweet Spot