Custom Attributes in Swift Language

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:

Image for post
Image for post

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.

Image for post
Image for post

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

Image for post
Image for post

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:

Image for post
Image for post

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.

Image for post
Image for post

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 :)

iOS Developer, speaker and educator. Top Udemy and LinkedIn instructor. Lead instructor at DigitalCrafts. https://www.udemy.com/user/mohammad-azam-2/

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