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
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.
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.
Keep moving foward: Handling non-optional optionals in Swift
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:
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.
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
Top comments (4)
Hi Eleazar, really nice article, but I have one question. If you create a singleton class and initialise all your view controllers from there? won't it never release from it's memory? it will never call
deinit()on the vc right?
I have started iOS development after Android and My thought is iOS is definitely swiftier than Java and I love it 😍
It really is 😂 Please stay tuned, there will be more cool posts.
Really good article. :)