6 BS Facts About Medium Writing Everyone Thinks Are True

There are a lot of people who still don’t understand the blogging ecosystem but go ahead and take a shot at educating others on facts they’ve “seen around” the web. When it comes to Medium Writing…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Code Generation in Swift

Discover use-cases of code generation and get an overview of the tools you can use to automate it

While working on the iOS project you can find yourself quite frequently writing boilerplate code. This is the code that you prefer not to write by hand and tempt to copy & paste most of the times. It can lead to certain mistakes and even crashes in runtime. In this situation, code generation might be useful. It lets you generate boring and repetitive code and make an application more reliable.

In this post let’s take a look at the use-cases when code generation makes sense and make an overview of the tools we can use to automate it.

First of all, let’s define the most common use cases for code generation:

It should be mentioned that Swift evolves, we don’t need code generation for certain tasks anymore. This includes automatic synthesis for Codable, Equatable, Hashable and CaseIterable protocols.

Unfortunately there is no an equivalent to Android’s R class in iOS. That’s why access to application resources like assets, localization strings, etc. is evaluated in runtime. This can lead to certain issues when a resource is missing in the application bundle.

In static languages like Swift unit testing with mocks is a bit problematic. The reason is that you can’t dynamically create or modify a class behavior in runtime. As an option, you can use OCMock library, but it is limited to NSObject subclasses and doesn’t provide full Swift support. We could use reflection, but it is quite restrained in Swift and is not suitable for creating mocks in runtime. The other options would be creating these test mocks manually by defining a class that conforms to a protocol. But it can result in a significant amount of tedious and repetitive work.

Luckily enough you can use tools to autogenerate mock objects that can be used in your tests:

With the help of those, you can automate writing and maintaining mock implementations, that you’ve done manually.

It should be pointed out that both tools use templates for code generation. You can provide a custom one if you are not satisfied with the default syntax.

Code generation is an effective way to increase your productivity and make your application more robust. You might be familiar with code generation in iOS and tried Sourcery or Swiftgen already. If not, I’d suggest starting with Resource accessors or Test mocks code generation. In this way, you can remove the need to maintain the boilerplate code manually and focus on other important things.

Thanks for reading!

Add a comment

Related posts:

Are You Fighting A Losing Battle Against Getting Rid Of Your Belly Fat?

Are you having a tough time losing your belly fat despite all your efforts including strict dieting and aggressive workout plans? Most men who are over 40 have this problem. Many think it is because…

Can I sue my own homeowners insurance policy for my slip and fall accident breaking my leg in my own condo?

Can I sue my own homeowners insurance policy for my slip and fall accident breaking my leg in my own condo? I own the condo with the homeowner insurance. I slipped on the wet ceramic floor and fell…

Lower East Side Tenement Museum

While I have visited the 97 Orchard Street location of the Tenement Museum before, touring 103 Orchard was a completely new experience. Walking through the recreated homes of 3 families who lived in…