How to limit a variable in Swift

Follow me, and we’ll put a vise on a variable!

Over the years, I’ve had a few instances where I needed to limit the potential of a variable. For instance, maybe I have a shopping app and I need to limit the maximum price of any given item. You may be surprised to learn that there’s no built-in way to tackle this in the Swift Standard Library. There are many solutions, but most come with glaring caveats.

Let’s start with a use-case

I have a UICollectionView. In this collectionView, I only want to display a maximum of 2 cells. This collection view’s count is based on an array that could have thousands of entries. …


How to refactor without hurting yourself

… and passing a closure


And what the heck does `layer is a part of cycle in its layer` mean???

And why you should…

Image courtesy http://www.tenordad.com/

“layer is a part of cycle in its layer”

Xcode barked at me as my app crashed. It was right after I created my first test view using a new Framework that my friend, Aaron Cleveland developed for programmatic Autolayout. I wanted to blame the way he was linking constraints in one of the convenience methods. But that was all standard best practice-y stuff there.

I looked over the convenience initializers I had added to his framework for UIStackView numerous times. Everything seemed in order there. “Hmm, the problem must be somewhere else in his framework,” I thought.

I headed over to StackOverflow and looked over this thread for…


Why you should use enums instead

“Magic” strings aren’t so magic in Swift

Stop letting magic strings make you look foolish

Using strings as a way of controlling the flow of an app, whether it be some condition you’re checking, an identifier, or a number of other things just isn’t a great experience. Especially as your app grows in size.

At one point or another, I know we’ve all spent time thinking, “what was the name of that identifier again?” … then you go back to look, and by the time you find it and copy it, you forgot what you needed it for in the first place. …


Demystifying the “R-Word” using Python

They’re not as scary as you think

In my many years of working with different programming languages, I’ve come across the “R-Word” (recursive!) more times than I cared to count. Until it clicked for me as to what recursive functions were doing — I always ran in the opposite direction!

Recursive? Uhhh... what?

Recursive: “relating to or involving a program or routine of which a part requires the application of the whole, so that its explicit interpretation requires in general many successive executions.”

Right, ok… Uhhh… what?

Don’t worry, I didn’t understand that either. The easiest-to-understand way I’ve heard recursive functions explained is:

“A recursive function is…


My Development Process When I’m Stuck

Problem Solving with UPER

I’ve been making apps for years. Mostly apps for friends and personal use, some with teams of developers, and I’ve worked on plenty of other people’s codebases. One thing is a constant in every one of these experiences — problems.

I sort of developed my own problem-solving framework over the years, and it was hit and miss. I was recently introduced to UPER through Lambda School and it’s changed the game entirely for me.

If you’re like I was, when faced with a problem, you probably do some form of restating the problem in a way that you can understand…


The Rock Is a Lie

Back to the days of Zork with you!

The first electronic game I ever played (Zork) was text-based…

Your imagination is the limit!

Maybe it’s because it’s reminiscent of my early days. Maybe it’s because I don’t have to worry so much about logic, and can focus more on the user experience.

Or maybe it’s because I just love the experience of watching my creation come to life, and when It comes to life from my words, it’s exactly how I imagined it in my head. When it’s visual, there’s always some compromise I end up having to make, and it no longer feels like 100% “mine”.

My inner-voice is very narrative. I…


It’s A Piece Of Cake With SwiftUI

And see how you can build a few more pieces to make a complete audio control

This didn’t take long at all — and I’m no SwiftUI Expert!

Building something as seemingly simple as a knob that you can rotate isn’t a simple endeavor using UIKit. Thankfully, SwiftUI makes previously complex UI operations a breeze.

Drawing a circle in Swift using built-in APIs is a fun exercise — but only because I’m a glutton for punishment.

Disclaimer: I don’t understand much about Geometry, so it makes it that much more difficult.

Thankfully, SwiftUI knows what a “Circle()” is

Want to make a circle in SwiftUI? Cool, just type Circle() anywhere you can render a view.


Don’t Worry, You Won’t Forget The One You’re An Expert With

Yes, Even If You Know JavaScript

Even if you know a language that can be used everywhere, sometimes there’s a much better way

Once I learned “Vanilla” JavaScript and got comfortable making network calls with node.js, I didn’t really think I’d ever need another language.

Years later, I actively work with 4 programming languages (excluding markup languages) two of which are Full Stack Languages (JavaScript and Swift). I’ve never turned down a job because it would mean working with a language I didn’t know… unless the prospective employer was unwilling to wait for me to learn. So while I only work with 4 programming languages actively, I’ve worked with at least 8.

Whether you’re self-taught, you studied…


Crashing your app is the safe route

Force-Unwrapping is safe!

Force-Unwrapping An Optional Is Safe Code!

You may think there is no safety net when you force-unwrap an optional. You’re telling the compiler, “I know what I’m doing, this value isn’t nil. I guarantee it.” If it is nil, your app crashes. What you may not know is this is actually a safe way to unwrap optionals.

“But Kenny, if I force-unwrap an optional and it contains a nil value, my app will just crash. This means it’s unsafe!”

Well, you’re right, but you’re also wrong

According to Swift Standard Librarian, Karoy Lorentey in his WWDC 2020 talk, Unsafe Swift, there’s no guarantee that your app…

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