Latest Post

How to Optimize Your Paid Marketing For Maximum ROI – Best Real Estate Websites for Agents and Brokers How to Triumph Over Budget Cuts and Prove Your Marketing ROI – c3centricity HOW TO MAKE DOG SHAMPOO

How to prevent ViewController initiation unexpected habits For iOS developers nowadays, there are many ways to develop ViewController. How do you create it?

By Storyboard, Xib file, or Programmatically? Mostly, you need to pass some parameters from current view controller to a new one that you simply created.? Assume that your view controller always require one specification” username”and you make certain that it has never ever been

nil so you state something like this Then prior to you navigate to that view controller you designate username and present it. It looks straightforward, simple, and great if you are solo task. But in the real life you have to work with your colleagues. More than one individuals for a single job. Picture your project does not have unit test. Someday your coworkers(or perhaps you)wish to change or refactor that code. Your associates (once again, or perhaps you since this code has actually been composed for a long time and you can not remember everything that you wrote) are unintended to remove this line

Yeah, you know what will occur next if you run this code and gain access to username again. Apps will crash due to discovered nil while unwrapping optional worth. Boom! So , it’s better if you have a way to let compiler knows this will be an issue and does not allow to assemble successfully. Shows put together error which file and line are cause the issue. Factory pattern and personal init come to save The above image is the system how Factory pattern works. You might believe like this

“I just desire to create a brand-new easy view controller so why I need to use something that looks made complex. Hmm” Yeah, I agree with you however keep calm and relax. We will just utilize a half of Factory pattern bypass by simply produce fixed function that initiation view controller, assign their required parameters, then return it. Lastly, do not forget to make personal init() of your view controller. That’s it.

Create by Storyboard:< p name="f370"id= "f370"class ="graf graf-- p graf-after-- pre" > Produce Xib file or programmatically So, the issue that I informed you above will be gone because you require your colleagues and even yourself to create a brand-new view controller by utilizing fixed function. No other way to develop like this So, we will always remember to assign “username” to our ViewController or erase this line anymore!

If you are producing a view controller by xib file (like me). You can utilize let instead of var for declaration username. It’s immutable, thread safe, high efficiency. You can keep your variable as private. You follow DRY due to the fact that of you constantly have to use just one function for creating a view controller. Nevertheless, you should not use segue if you are using storyboard since it will break rules. It’s your choice to choose which one is fit with your requirements.

Call to Action Thanks for your time to read this short article. I hope it will be useful for some iOS developer guy worldwide. If you have any question, do not hesitate to comment below or say hello in Twitter @itopstory. I like to share understanding that I know in both Thai and English. See you again next time.

P.S. This is my personal blog site.