Better Programming

Advice for programmers.

Follow publication

Member-only story

24 Swift Extensions for Cleaner Code

Alisa "Foxicorn" Nekrasova
Better Programming
Published in
6 min readAug 19, 2020
Photo by Francesco De Tommaso on Unsplash

One of the best features of both Swift and Objective-C, in my opinion, is extensions. They enable you to add new methods to any class and the whole project will be able to call them without additional inheritance and overloading.

As a mobile developer I work with both iOS and Android, and I very often see Android functions and methods which are shorter, cleaner and more understandable than in Swift. Using extensions, some of these methods can be ported to Swift. Plus with several new methods (extensions) we’ll get a short, clean and easily maintainable code in Swift.

I’ll use Swift, but all these extensions can be migrated to Objective-C or used with Objective-C directly, without conversion.

String.trim() and Swift.trimmed

In 99% of the cases when I trim String in Swift, I want to remove spaces and other similar symbols (for example, new lines and tabs).

This simple extension does the trick:

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Alisa "Foxicorn" Nekrasova
Alisa "Foxicorn" Nekrasova

Written by Alisa "Foxicorn" Nekrasova

Game and software developer with more than 17 years of experience. Freelancer, writer, author of the book “Swift Recipes for iOS Developers”.