If you aren’t careful, you can mutate constants!

Good discipline should be more of a concern than using a value type over a reference type

If you’re doing is using structs over classes, your code isn’t necessarily safe

If you’ve been doing Swift Development for any length of time, I’m sure you’ve heard that it’s good practice to use a struct over a class by default. While this is true in many cases, and for good reason, it’s very easy for a struct to be just as unsafe as a class. Read on to learn more.

Why do people say to use structs over classes by default?

Common advice given to Swift Developers is to use a struct by default, and only use a class when you have to. Let’s explore the reasons why and see why this line of thinking isn’t necessarily correct for all situations.

Structs are value types

Structs, being…

Decoupling code with protocols and delegation

You don’t pack the whole house when you go on vacation, you bring a suitcase

A bunch of suitcases on display
Um, you’re only leaving for a few days, do you really need all that? (photo credit: arthur-edelmans, unsplash.com)

Imagine you’re going on vacation. You know exactly where you’re going, how long you’re staying, and the weather pattern is very reliable. You’re super prepared for what’s to come, and you know exactly what you’ll need when you get there. It wouldn’t make sense to pack everything you own and bring it with you, would it? Yet a lot of us do just this when we don’t use the delegate pattern with a protocol.

No architecture needs to have a massive view controller

Write a fully functional ViewController in about 30 lines of code

Small, blurry background text with the words “very tiny” magnified
Tiny ViewControllers? Very tiny

So you’re sick of massive view controllers? Me too. Many architectures attempt to overcome this problem in a way that I view as extremely complex for the problem they’re trying to solve.

I favor simple architectures and simple solutions because they’re easy to understand, develop, and maintain. For this reason, I normally start my projects using the MVC architecture. I find for most projects it strikes a great balance between complexity and abstraction. It keeps things well-separated by giving the model, view, and controller layers their own concerns.

A lot of Developers, though, complain that MVC leads to Massive View…

From verbose to concise

From heavy to lightweight

A hand touching a large digital display of some sort
Photo by Elia Pellegrini on Unsplash

Say you’re making a singleton or static class to represent data and/or logic that will never change. In many cases, you can convert this logic to an enum and save yourself a bunch of hassle. An enum has much of the functionality of a class, but doesn’t require an instance and doesn’t allow for stored properties — so doesn’t manage state. Instead of properties, you get cases. Instead of values, you get the RawValue. In this tutorial, you’ll refactor an existing static class into an enum.

What Is a Static Class?

A static class is a class that contains only class/static methods and properties. It’s…

The single responsibility principle in action

Implementing a reusable programmatic UICollectionView

Your ViewControllers don’t have to be so massive

In this tutorial, we’ll implement MVC in a way that you may not be familiar with. We’ll create a reusable UICollectionView that’s implemented by its parent UIView and delegates item selection events to a UIViewController. This makes for a very small view controller when compared to the default way we tend to implement them. In addition, it gives you an entirely reusable view that you can invoke with just a single line of code.

This tutorial will cover separating concerns using MVC. If you don’t like MVC, don’t like the fact that I used a view model, or don’t like…

Is async/await ready for production?

Introduced at WWDC 2021, it will clean up your existing async code… with a little help

A person sitting on the couch, waiting for a text

A lot of us have been waiting a long time for async/await syntax in Swift. First proposed in 2017, this API sets out to clean up the pyramid of doom that happens so often with async code. In the WWDC State of the Union, we were introduced to some amazing-looking async code using this syntax. If you’re a professional iOS Developer or learning to become one, you may be wondering if it’s ready for production.

̶I̶M̶H̶O̶ ̶t̶h̶e̶r̶e̶ ̶a̶r̶e̶ ̶s̶o̶m̶e̶ ̶t̶h̶i̶n̶g̶s̶ ̶t̶h̶a̶t̶ ̶n̶e̶e̶d̶ ̶t̶o̶ ̶c̶h̶a̶n̶g̶e̶ ̶b̶e̶f̶o̶r̶e̶ ̶a̶d̶o̶p̶t̶i̶n̶g̶ ̶t̶h̶e̶ ̶f̶r̶a̶m̶e̶w̶o̶r̶k̶,̶ ̶b̶u̶t̶ ̶u̶l̶t̶i̶m̶a̶t̶e̶l̶y̶,̶ ̶t̶h̶a̶t̶’̶s̶ ̶a̶ ̶d̶e̶c̶i̶s̶i̶o̶n̶ ̶y̶o̶u̶ ̶a̶n̶d̶ ̶y̶o̶u̶r̶ ̶t̶e̶a̶m̶ ̶n̶e̶e̶d̶ ̶t̶o̶ ̶m̶a̶k̶e̶…

Take your development to the next level, eliminate your assumptions with Unit Tests

Stub -> Fail -> Implement -> Test/Implement/Test Until Pass -> Refactor -> Test/Refactor/Test Until Pass -> Repeat

Don’t you wish you had an automated way to test your code every time you make a change? Good news! You do!

If you’re anything like me, you make a lot of assumptions when you develop software. Also, if you’re anything like me, you probably didn’t realize it until recently, or maybe you still don’t realize it. Trust me when I say, “assuming you aren’t making assumptions is the worst assumption you can make.”

In this article, I’ll introduce you to Test Driven Development by implementing a very simple method and test case. Let’s say we’re developing an e-commerce app and we need to devise a way to add money to the cart. Following the single responsibility principle, this method will only

Increase performance by avoiding retain-cycles

Track down this elusive time-waster in a flash

vertically scrolling numbers, dripping/leaking down the page
Ew, leaky code

In Swift, memory is handled for us pretty efficiently via ARC. Basically, reference objects (such as classes) hold a count for how many times they’re being referenced. When the count reaches 0, the item is marked for deallocation and removed from memory when the system needs the space.

If we aren’t careful though, we can encounter situations where the memory space never gets marked for deallocation because we’ve created a retain cycle. Unfortunately for us, these can be pretty tricky to track down.

In this tutorial, you’ll implement a unit test that can test any object to see if it’s…

A good API doesn’t break easily

Designing Swift Code with Enums

A little blue character with a repair kit looking at a hole in a website or app
Keep those holes closed, so your users don’t experience interruptions!

Often when developing codebases, you probably want to model a situation with one of 2 possible outcomes. In Swift, this often means using optionals as the return type for a method. The idea is one of them will be nil, and the other will be non-nil.

What you may have done in this situation is create a method that returns a tuple with 2 optional parameters. The one that isn’t nil is the one that passed the test. This solves the problem of wanting to know which contains a value. Unfortunately for you, this creates a new problem.

The factory pattern keeps your code clean. Enums keep data flexible

Encapsulate your static initialization data — keep your call sites and objects clean

They’re all boxes, but they might have different labels. How do you handle such a case?

One Type, Different Property Values

Often times when creating a custom type, you’ll want to create many instances of that type with different initialization values. For example, if you’re modeling a Vehicle, you may want different vehicles with a different number of wheels, different colors, different body styles, etc…

To achieve this, we often make our initializers flexible by being able to pass in whatever value we want for each property we’re initializing. While there’s nothing wrong with this solution, it can lead to a lot of boilerplate code at call sites…

Kenny Dubroff

I’m a Full Stack iOS Developer with a passion for writing clean, reusable code.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store