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.

Stay in touch

Thanks for reading this article. I'd love to stay in touch and share more tips on programming and side projects with you. Sign up and I'll send you my articles straight to your email, you'll also get a free copy of the light themed version of my Git cheat sheet.
Git cheat sheet preview image