Reusing files across iOS projects


Throughout each project, I add and tweak reusable files, such as categories and view classes. However, I have certain issues to deal with: how do I get the old files prefixes to match this new project? How can I move settings from one project to another? In this post, I’ll show you how I do things, and hopefully you can use some pieces of it for when you’re starting your next iOS project.


Dealing with prefixes

I like my reusable files to have the same prefix as the current project I’m working on. For example, if I make a category file for my VOX project with the prefix VOX, NSString+VOX.m, I want to reuse it in my LIV project as NSString+LIV.m. To do this, I keep a folder of all of my reusable files that I copy into each new project. Everything in this folder is prefixed with PREF. Then, I replace all occurrences of PREF in my files and file names, using this code, which I keep in my .bashrc file:

replace_text () { for file in $(grep -rl “$1″ .)
sed -e “s/$1/$2/g” $file > /tmp/tempfile.tmp
mv /tmp/tempfile.tmp $file
done ; }

replace_file_names () { for i in `find . `
do mv “$i” `echo “$i” | sed -e s/$1/$2/g`;

batch_replace () { replace_text $1 $2 ; replace_file_names $1 $2 ; }

To run this, I call `batch_replace PREF ROL` if I want to set it to ROL. It’s not perfect, but it generally works. When the project is done, I move all of the files from the reusable code folders back into my shared files folder, run a batch_replace command to set them back to PREF, and then push my new folder to git. Here’s my git repo:


What to reuse?

In general, I try to reuse my settings (.xcconfig files), my .gitignore, my Podfile with some common pods, and lots of Objective-C files, such as my beloved categories. Try out my UIView+PREF.m, NSObject+PREF.m, and NSString+PREF.m files if you’re curious about cool uses of categories!


Putting it all together

Starting a project:

  1. Make a new project in Xcode
  2. copy all of the files in ~/xcode_shared_…/ over to the project (before actually dragging them into xcode, so we can do the replace first!)
  3. run `batch_replace PREF <prefix>`
  4. Drag all of the folders and files into xcode. Make sure that they’re in the right spots, e.g., .gitignore and Podfile should be in the project root.
  5. install cocoa pods by moving .cocoapods into the main folder, making sure the correct iOS and stuff are specified at the top of the file, then running `pod install`
  6. open <app name>.xcworkspace
  7. install crashlytics, mixpanel, and parse (be careful in following the crashlytics steps)
  8. add the following step to the build phase storyboardlint “$SRCROOT” –storyboard-suffix Storyboard –segue-suffix Segue –reuse-suffix ReuseID
  9. use the two .xcconfig files to get the settings right (AFTER cocoa pods is installed)

Finishing a project:

  1. Copy all of the files from shared folders, e.g. the Categories folder, back into the shared files folder
  2. Copy the .xcconfig files over, if there were changes I made that I want to reuse
  3. Remove all of the files from Podfile that I don’t want to reuse
  4. Run a `batch_replace {prefix} PREF`
  5. git push the new changes

Reverse a String 4x Faster

Naive string reversal (e.g. is simple and might feel optimal. However, if we treat a string as if it were an 8-byte array, then we can do a reversal with an eighth as many swaps. With this in mind, we’ll do the same reversal algorithm, but this time on 8-byte chunks. This doesn’t completely reverse the list, though. We need to reverse each 8-byte piece about itself. Fortunately, we can reverse the bytes quickly with our friends, >> and |. Finally, we’ll have a small piece left over in the middle that needs to be reversed in smaller chunks.

EDIT: A lot of people have mentioned that this code only works for ASCII strings. However, since UTF-8 and UTF-16 strings are usually reversed by first reversing each byte (or two bytes) and then reversing each unicode sequence (, this algorithm can be used in reversing them as well.

Here’s the code:

void strrev(char *str, int len){
	//temp swap variables
	int i = 0, j = len / 8 - 1, endpoint = len / 16, temp1, temp2;
	unsigned long long int *str_i1 = (unsigned long long int*)str, *str_i2 = str + len % 8; //separate arrays used to deal with alignment 
	while(i < endpoint){
		temp1 = str_i1[i];
		temp2 = str_i2[j];
		//reversing the first
		temp1 = (temp1 << 32) | (temp1 >> 32);
		temp1 = ((temp1 & 0xFFFF0000FFFF0000) >> 16)  |  ((temp1 & 0x0000FFFF0000FFFF) <> 8)   |  ((temp1 & 0x00FF00FF00FF00FF) << 8);
		//reversing the second
		temp2 = (temp2 << 32) | (temp2 >> 32);
		temp2 = ((temp2 & 0xFFFF0000FFFF0000) >> 16)  |  ((temp2 & 0x0000FFFF0000FFFF) <> 8)  |  ((temp2 & 0x00FF00FF00FF00FF) << 8);
		str_i1[i] = temp2;
		str_i2[j] = temp1;
	//for the middle piece (always less than 16 bytes), we use a naive reversal
	char temp_c;
	i = len / 2 + (len % 16) / 2;
	j = len / 2 + (len % 16) / 2;
	while(i < len / 2){
		temp_c = str[i];
		str[i] = str[j];
		str[j] = temp_c;

This runs about 4 times faster than my naive implementation using the O3 flag with gcc.

The code I used to actually compare things is here: