Helpful iOS and Xcode Code Snippets

Organize your code and create new snippets to use in your codebase

Matias Jurfest
Sep 30 · 3 min read
Photo by chuttersnap on Unsplash

Writing clean and readable code is always important, but organizing it well is too.

The best way to separate your code within a file in iOS development is using MARKs, and instead of writing these MARKs every time, you can just use a code snippet like this one:

UIViewController MARKs code snippet

This code snippet shows a way to separate your UIViewController into sections. Depending on your architecture, it may vary which MARKs are necessary. This snippet is for a simple MVC architecture.

Another useful code snippet may be used in the models:

Model MARKs code snippet

Note that the dash (// MARK: -) adds a nice separation line between MARKs in the jump bar. On Xcode 11, you can see the same separation in the Minimap view.

Jump bar view of MARKs

Benefits of MARKs Code Snippets

Create Your Own Code Snippet

Any code can be a code snippet, not only MARKs. Actually, each of the benefits applies for every code snippet.

If you want to create a new code snippet in Xcode, you just need to select a portion of code, right-click on it and choose the option Create code snippet. Then, you can edit and delete it by pressing cmd + shift + L.

Xcode 11 UI to view, edit and delete code snippets

How to use them

There are two ways:

Share It With Your Team

Open ~/Library/Developer/Xcode/UserData/CodeSnippets and send all your .codesnippet files, or add them to a GitHub repo.

After adding the files to the folder (if the folder doesn’t exist, create it), restart Xcode, and start using the snippets for all your projects.

Available Code Snippets

Before creating your own code snippets collection, check out mine in this GitHub repo. You are welcome to contribute.

Better Programming

Advice for programmers.

Matias Jurfest

Written by

Software Engineer & iOS Developer | Uruguayan based in Tel Aviv

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade