Discover #error()


You’re more of a video kind of person? I’ve got you covered! Here’s a video with the same content than this article 🍿


Have you ever used #error() in Swift?

If you're an SDK or an API developer, you might find it quite useful!

Let me explain why!

Imagine you need to create a sample project that shows how to use your SDK or your API.

The first thing you probably want your users to do with the sample project is to update it with their own API key.

So you need a way to easily and immediately direct their attention in that direction when they first open the sample project.

And this is when #error() comes to the rescue!

By adding #error() at the place where users should write their API key, you will force the compiler to emit a compilation error, which is guaranteed to catch your users' attention and direct them to the place you want!

That’s all for this article, I hope you’ve enjoyed it!

Here’s the code if you want to experiment with it:

import Foundation

#error("You can get your apiKey at https://dev.myapi.com/")
let apiKey = "create_your_own_api_key"
Previous
Previous

Discover CollectionOfOne

Next
Next

Here are 5 Xcode tips you really wanna know 😌