Custom Attributes in Swift Language

Mohammad Azam
2 min readJan 25, 2017

Move along there is nothing to see here!

Currently, there is no support for custom attributes in the Swift language. You might have played around with built-in attributes and some of them are shown below:

But you cannot create your own attributes. I believe that custom attributes can provide a great benefit to iOS developers and Swift language in general. Let’s take a look at few use cases.

Validation

Custom attributes can be used to provide a cleaner way to validate your view models.

You should even be able to use multiple attributes to validate a property as shown below:

User Interface Elements

Custom attributes should be able to control how your model is rendered on the view. Take a look at the following example:

This provides the flexibility if you want to change the look and feel of how one piece of information is displayed. All you need to do is change the attribute and the user interface control will be rendered to a completely different custom control.

JSON

A common scenario is when the server sends JSON back to the iOS app and the app has to bind that JSON to the correct model. Custom attributes can allow to create a simpler and seamless binding experience.

These are just few use cases for custom attributes. I believe custom attributes if introduced in the Swift language would be a can of candy for developers :)

--

--

Mohammad Azam

Lead instructor at a coding bootcamp. Top iOS mobile instructor on Udemy. Author of multiple books and international speaker. azamsharp.school