Top 5 Coding/Programming Tips For Swift

Start with Playground:

Using playgrounds in Xcode is a way to quickly program some code to check for validity since playgrounds has a feature that shows you the result of the statement written. This is a handy feature to learn about Swift new features and newer syntax since every Swift version comes with a newer slightly different syntax. This feature is available as soon as you open Xcode, then go to File -> New -> Playground…

Usage of Enumeration:

Enumerations in swift adopt many features that are traditionally supported only by classes. Good examples are instance methods used to provide functionality related to the values the enumeration represents and computed properties to provide additional information about the enumeration’s current value. Enumerations can be provided a raw value for each case, this includes strings, characters, or any numerical integer.

Source: https://docs.swift.org/swift-book/LanguageGuide/Enumerations.html

Having Constants in a Single File:

One of the best tips to help reduce time to initialize a constant value and have to reference it in areas of your code can be considered time wasting and something that can be easily lost in the code depending on your programming techniques, the use of creating a constants.swift file can allow all those constants to be in a single file within a struct that allows for nesting that can be used to assign namespaces to constants.

These constants can be later referenced on other viewControllers by typing Cells.CellIdentifiers.RedBloodCell

Using IBInspectable:

This feature allows for attributes to be assigned for your UI components in a single block of code, instead of using the User defined runtime attribute, and by assigning the new class to a UI component, you will receive new features that can be used to further customize the UI element.

This will create an extension of attributes for UIViews in your storyboard, as shown in the next image.

Using Cocoapods:

Cocoapods is a tool that allows the user to integrate external code into your own iOS projects.  These third party libraries help the project move along faster and allows for very complex algorithms to be handled by the third party library while all you have to do is pass the data that you want that code to use. This is where cocoapods can help, there will be situations where managing multiple libraries will eventually cause problems since some may share a dependency. Cocoapods allows for adding and removing dependencies with a single command that uninstalls or installs them, all the user has to do is specific the dependencies that the libraries have.

Like the post? Share it:
David Lopez
David Lopez

Motivated and proven developer in both Mobile and Web Development. Developed Web Applications in PHP, JS, HTML, Java, Swift, C#, and Angular. Highly experienced using AWS and proven with a certification for Developer Associate.