4 things I've learned after 10 months as an iOS developer
Tags: swift, ios, mobiledevelopment, beginners
Published: 2018-07-23 14:13:20

---
title: 4 things I've learned after 10 months as an iOS developer
published: true
description: 4 tips for iOS/Swift developers
tags: swift, ios, mobile development, beginners
cover_image: https://thepracticaldev.s3.amazonaws.com/i/klm9prz0ja90iic6157a.jpg
---

A few years ago I started my journey in the world of native mobile development using the android environment and java. Then, due to requirements at the company I was working with, I saw myself in the opportunity to start as an iOS developer and since then 10 months have passed. It’s been a long journey that never ends and today I’ll tell you 4 things that this beautiful experience has taught me. What I’m going to present below are just some tips that I've been learning on the fly and that I implement in my day to day. I hope you find it useful, let's get started!

#1- MVC also known as Massive View Controller

Apple's MVC in the long-run does not work. Your viewcontroller sooner or later ends up doing a lot more than it should and that will end up in a class with 600 lines of code that is very hard to follow. It's better to use some design patterns on top of the traditional MVC Apple provides. There are many options out there: VIPER, MVVM, MVP, etc. It will depend on you and the specific needs of your project. Some time ago I wrote an introduction to MVVM using swift, please take a look:

[MVVM pattern sample in swift/ios]
(https://dev.to/eleazar0425/mvvm-pattern-sample-in-swiftios--58aj)

#2- Avoid non-optional optionals!

The optionals are one of the most powerful Swift features without any doubt however the developer doesn't always use it in a suitable way to the use cases. I've noticed that it's very common to use optionals that really are not optionals. I mean, when a property is 100% required for our program to work, is it really an "optional" at the end of the day? Let's see this in an example. Suppose you have a basic UIViewController, which consumes a service that populates a tableview. For this viewcontroller to work correctly, you must have assigned the 'service' property and the 'tableView' property as well.

{% gist https://gist.github.com/eleazar0425/e3e8728b6d56b4c5a42983e812732574 %}

It's tempting to put these attributes as optionals and pass them from outside. However, what happens if these properties are not assigned? **The user will visualize an undefined state in the ui.** Here's a subject that is very debatable but in my opinion it's better to get a runtime/compilation-time error than showing an undefined state to the user, because at least the developer is able to handle the exception. And you can achieve this by using implicitly unwrapped optionals.

{% gist https://gist.github.com/eleazar0425/51c237a749b83f047fc456ded4712b98 %}


Keep moving foward: [Handling non-optional optionals in Swift]
(https://medium.com/@johnsundell/handling-non-optional-optionals-in-swift-e5706390f56f)

#3- Stop using segues!

When you have to pass data between viewcontrollers and your application is having a slightly more complex flow, it can all become very tedious. One way to handle the routing is to use segues in the interface builder, but I've realized that it's not always the best way to handle it and I'll explain why. Let's take a look at this piece of code:

{% gist https://gist.github.com/eleazar0425/404d33fb6f1e01c6725e069a02a2d2e7 %}

This code is difficult to read, difficult to maintain and is also very sensitive to changes because if someone modify an identifier or the viewController type the whole project could become a disaster. Some developers prefer to create the viewcontroller from scratch using swift and xib files but the approach that I like the most is to use a middle point between both forms and use another class that allows us to manage the routing in an efficient way. 

By using wireframes you can achieve a better maintainable and testable code, have all the logic of routing in one place (single responsibility principle) and also prevent your application from collapsing if someone changes the segue identifier.

{% gist https://gist.github.com/eleazar0425/d6b78b3d3cb6bcea3f04caab2c8c2c6d %}

Cleaner, simpler and a better way to handle errors. It also helps you avoid the massive view controller that I mentioned in point number one. I will write more about this in the future so please stay tuned.

#4- Swift is sexy, take advantage of it!

Coming from a strong background in java, many of you will be able to understand why swift could seem to me as such an attractive language. Swift implements some powerful features and some functional paradigm features that are really useful. One of the things that I've learned is how powerful Swift is and the more you take advantage of it the more powerful it becomes.So I'm going to finish with several links to learn (and keep learning) Swift and iOS. And remember, we never stop! Every day we can learn something new that leads us to be better professionals.

[The Swift Programming Language 4.0](https://itunes.apple.com/gb/book/the-swift-programming-language-swift-4-0-3/id881256329?mt=11)

[Developing iOS apps with Swift](https://itunes.apple.com/us/course/developing-ios-11-apps-with-swift/id1309275316)