How to detect orientation change (Swift 3.0)

Let’s study this example: every time you rotate your iPad, you need to recalculate cell heights in your table view. You only need to do this on iPad, because iPhone version of the app is going to have only portrait orientation.

So, we need to detect orientation change. Once orientation change is detected we can call tableView.reloadData() method. Let’s code.

First of all, we need to add an observer to our view controller. Add this line in viewDidLoad() method:

Also, implement rotated() method.

You only need to detect orientation change between two orientations: Landscape and Portrait. But there are a couple more device orientations. We also have FaceUp and FaceDown orientations. We don’t want to reload data in table view every time we go from, say, Landscape to FaceUp, because in that case the interface orientation shouldn’t change.

Also, if we change orientations like this:

Portrait – FaceUp – Portrait

we don’t want to reload table view.  So, when we go from FaceUp to Portrait again, we need to know that the last orientation that counts was Portrait also. To store the last orientation we need a variable.

Create this variable in your view controller:

Instantiate it in viewDidLoad():

Now we can rewrite rotated() method:

So, that’s how it is done. Or at least how I did it in my Stream Journal app. I wonder if it is the best possible way to detect change between Portrait and Landscape. What if, instead of device orientation change, we could detect user interface orientation change directly? That would save us all the additional coding. If you know of such a way, please, comment.   

Here is something free for you

Don’t close this page yet. Before you go, maybe you’d like to check out my app. If you want to control your life and be focused on your goals this app is definitely for you. You create your own list of questions and then answer those questions every day. And recently I added the ability to create multiple lists of questions.

  Check it out on App Store


How to keep your ass healthy as a developer

I once read an article about some recent research on preventing negative effects of sitting. I don’t have the link now but there are a lot of articles on this topic, and you can Google them if you are interested. So, what was it all about?

I’m sure, you know that prolonged sitting has negative effects on your health. Sitting is a new smoking, as they say nowadays. Different studies have shown that sitting for extended periods of time increases risks of a number of health issues, among which are heart disease, diabetes and cancer, to say nothing of some ‘lighter’ stuff like hemorrhoids and prostatitis.

As a developer, and previously a civil engineer working at the office, I sit a lot. So, the health risks associated with sitting have always concerned me. That’s why the article I mentioned above interested me in the first place.

What that article suggested, based on some recent study, is that to prevent most of the negative effects of sitting all you have to do is to walk for 2 minutes every hour. It didn’t explain why it was so, but for me it made a lot of sense. And here is why.

It is easy to imagine that when you sit, some of your internal organs are compressed and blood doesn’t flow normally in some parts of your body. That might cause no harm if you sit for just a short period of time. But if you sit for prolonged periods of time it is once again easy to imagine that some negative processes start to occur in the compressed part of your body. Like some cells dying off. Or whatever. I’m sure it’s more complicated than that.

But what if you don’t wait till something bad starts to happen in your body and start walking after one hour of sitting? Then you blood starts to flow normally, your internal organs relax. And maybe 2 minutes of walking are really just enough to prevent some of the negative processes in your body from happening. Then you can sit for another hour and repeat.

On the other hand, if you tried to offset the negative effects of sitting by just doing some exercises after work hours, it wouldn’t help. Because by that time you would have already done the damage to your body from uninterrupted hours of sitting.

How I implemented this in my life

I just have the alarms in my iPhone set for every hour of my work day. When the alarm goes off, I would stand up, immediately set a timer for 2 minutes, and start walking around the office. At first I was a little self-conscious. What would my colleagues think of me wandering here and there 8 times per day instead of sitting and working like everyone else? But I figured that my health is more important to me than what anybody’s opinion of me might be, so I kept doing my little ritual. The funny thing is nobody cared. I never had to explain myself. Well, actually, I did explain what I was doing to one of my colleagues, but I really didn’t have to, because he didn’t ask me.   

And now that I work from home I do just the same thing. I use alarms and timers and walk for 2 minutes every hour. I suggest you try it if you are interested in staying healthy.


Is programming a right choice for you? (career advice)

Can you become an iOS developer?  Or a any other type of programmer? No. You can’t. Just kidding.

If you are not a programmer yet and are only thinking about going into IT, then this article is for you. You see, when I was in that situation (when I worked as a civil engineer) I had my doubts. I wasn’t sure if the IT was the right thing for me. And I had invested a bunch of years into my engineering career already. Do I just throw all those years away?

Also, when I’d chosen my first career I thought it would be a perfect fit for me. I was wrong. What if I was likewise mistaken about programming? What if I was deluding myself for the second time?

As it turned out, programming is a much better fit for me than engineering. But the funny thing is that it now  seems to me, if I only knew the things I know now, let’s say several years ago, the transition to programming could have become unnecessary. I could have made it in engineering. I could have found an interesting and fulfilling job there.

Now I think that whether you are fulfilled at your job or not doesn’t depend only on the type of work you are doing. It has more to do with your whys. I spent most of my twenties not knowing what I want from life. I didn’t have any goals. Now I do have goals. And I imagine that if I started out not as an engineer but as a programmer right from the beginning, without goals, I might have become just as miserable a programmer as I was engineer. And on the other hand if I had goals right from the get go, I could have ended up a pretty good engineer, fulfilled with my work. Who knows?

So, if you are in a similar situation, if you don’t like your job or profession, ask yourself, is your job really a problem or maybe the problem is you, or more precisely your lack of goals.

Having said that, I still don’t know what whys I could have found for myself in the field of construction engineering. But I can tell you some of my programming whys.

Benefits of being a programmer

When I finally quit my engineering job and decided to learn iOS development I was considering two major benefits programming was going to bring into my life.

First is location independence. Second is the potential to make passive income. First one needs no explanation. If you are a programmer, theoretically you can work from anywhere in the world. All you need is a laptop and internet connection. As of now, I’m not sure if traveling is that important for me. I’d say that I like the idea of freedom to travel anywhere I want anytime I want more than I actually like traveling.

As for passive income, my thoughts were as follows. In order to make money you provide value to the marketplace. And if you want to get money passively all you need to do is to build a system that will provide value to the marketplace passively. And what is software? It is exactly that. A system that provides value passively. When it comes to providing value, software doesn’t differ much from any other thing made by man. Almost everything people create is designed to be useful in one way or another. But software, on top of that, is also designed to  work passively. Basically it works on its own and doesn’t require much maintenance once built. So, if you are interested in building streams of passive income it wouldn’t hurt you to learn programming. Basically every programmer is a professional creator of money machines. Ironically most of the programmers have to work just for their wages making money machines for other people.

Other benefits of programming

So, those were two things that excited me about becoming a programmer. Later I found other benefits of being a programmer. Like this one: I discovered that as a programmer you can show your work easily. You can build something – a site, an app – release it and share it with the world. Now everybody can see what you are capable of doing.

It wasn’t like that in engineering. Theoretically I could have stolen some CAD drawings from my previous employer to present them on my next job interview. Or I could have created some fake project. But it’s too much work and it would still be something fake. In construction engineering you can’t just create something on your own. Some authority has to approve your creation, otherwise it doesn’t count.

The situation in programming is totally different. For example, when I was learning iOS development, I spent one year unemployed, but I still had a lot to show for that year, because during that year I created several apps. Apart from being able to show your work easily you can work on your own projects just for the sake of honing your craft. For example, in the company I currently work for, we haven’t started using Swift yet. So I write my own apps in Swift. That’s how I keep my skills up to date with what is going on in the industry. Also, creating something on your own is just so interesting and fulfilling in itself. I found out that no matter how many hours a day I overwork on my main job I always have energy left for my own projects.

Basically what I’m trying to say here is that programming as an industry provides a very friendly environment for a creative type of person. And with a little bit of hard work your can get everything you ever wanted: autonomy (did I mention, I work from home?), location independence, money, creativity, heck, you can even start your business and be your own boss. I want all of that. That’s what motivates me to do my job.

If all those things motivate you too, than programming might be a good choice for you. Anyway, you need to know you whys. Without them you are going to be miserable in any profession.

Is programming a good fit for you?

I can’t tell you that. But I can tell you how I found the answer to that question for myself. Maybe you’ll even get something useful out of it. At first I had doubts. I wasn’t completely new to programming. I have programmed some stuff occasionally all through my life. It was just for fun and it wasn’t systematic. All I knew was that I liked to code. But I wondered if I would feel the same about programming if I was forced to code every day for 8 hours. There was no obvious answer to that. Also, how would I feel if I had to work on projects I might not necessarily care much about. So, I spent a lot of time hesitating.

But when I finally made a decision and started my new career, I found out that programming was a much better fit for me than I’d ever imagined. The demands this profession puts on a person matched a number of my personal qualities. Here is a list of them.

  1. I’m smart. OK, it might sound arrogant. What I mean by that is that I can figure things out. Like in a geeky way. I may be dumb in many other ways, but when it comes to learning new stuff, it just comes naturally to me. All through my school and university years I was a good student. All the cheaters in the class copied my homework. And working as a programmer can be compared to being a student. You just show up, do your tasks and get good grades. I’ve done that successfully for many years. So, if you were not good at school, maybe, just maybe, programming might not be your thing.
  2. I like to learn. Remember, I told you that in my twenties I didn’t know what I wanted to do with my life? In that period of time I had a hobby which was to learn languages. I’d learn Spanish, French, Armenian languages to various degrees. I even started to learn Chinese language. Then it all ended when I decided to learn programming. I figured why learn languages that you don’t even use when you can learn programming languages and get paid for it.
  3. Technical background. Sometimes in my work I take on tasks of such difficulty that I wonder if I’d be able to solve them without my technical education. But I have to say, such tasks are very rare. Most of the time you can just google your way to the solution of a problem.
  4. Communication skills. I always knew that I was an introvert. And it was so bad that people actually made remarks to me about that. They asked me why I rarely smiled and why I didn’t talk much, which made me uncomfortable. When I started working in an IT company all that changed. There are so many weird people in IT that I feel like a normal person for the first time in my life. In my new profession I had to deal with people who couldn’t talk normally. Usually you talk to such a person not because you want to but because you need to get some piece of information from him. And he just can’t express his thoughts. He doesn’t even look you in the eye when he talks. You can see how painful it is for him to talk to you and you start wishing you didn’t have to deal with that person in the first place. So, communication is important in programming. And I have just enough communication skills for this job.

I realise that this entire post was about me. But I wrote it hoping that it could be useful to other people also. Even though I’m talking here about me and my experiences, you can still get some insights from this article. You can compare your situation with mine or your personal qualities with mine and try to figure out if programming might be the right choice for you.

And finally I’d like to talk about another benefit I get from programming. And it is recognition. When I started working as an iOS developer, my colleagues immediately started to praise me for my work. They were surprised at how good my code was and how fast I could write it. They praised me so much that it even made me feel uncomfortable. For some time I even thought they were kidding or making fun of me. But they were not. It just so happens that programming is such a good fit for me, that I can actually realise my potential in this profession and not be indistinguishable from the masses like I was in my previous job.

I can give you an advice. If you want to change your profession or you are a young person just trying to figure out what you want to become when you grow up, pick a field not just by the benefits it can bring you. Pick a field that matches your personal qualities. Pick a field where you can become the best. And don’t aim to be average. Aim to be the best in your field. If you doubt that you can be the best in your field, probably you picked the wrong field. Whatever your pick, hard work is required. But it is better to work hard to become the best, than to work hard to be average.

But then again I already told you that I feel I could have made it in engineering if I only acted differently. Maybe this whole career picking is not that important. Maybe you just have to pick something and put in the hard work.

Here is a good book you can read on this topic.


Xcode crushes every time you open it. How to fix it?

I had this problem today when I decided to open Xcode and start working. It just crushed every time I tried to open it. What to do in this situation? Easy. Just delete xcuserdata from the project. To do this:

  1. go to your project folder in Finder
  2. right click .xcodeproj file (or .xcworkspace file if you use Cocoapods) and choose Show Package Contents
  3. delete xcuserdata folder

How to calculate height of a multiline String in Swift

Yesterday I spent like 2 hours trying to find answer to this question. Eventually I had to figure it out on my own using a bunch of Stackoverflow answers, each of which, if used alone, didn’t produce the desired result.

So, here is what I came up with. It works!



Popping to specific view controller (Objective-C)

I just wrote this piece of code for the project I’m currently working on.

You can use it if you know for sure that view controller of SpecificController class exists somewhere in the Navigation stack. Otherwise your app will crush when index goes down to -1.



How to save an image to photo library (Swift snippet)

This is how I did it in Sigma app with Swift 3.0



How to send an image via email (Swift snippet)

This is how I did it in Sigma app with Swift 3.0.

First you import MessageUI. Then you declare that your class adopts MFMailComposeViewControllerDelegate protocol. Then implement these two methods.



How to free a lot of space on your development Mac computer

Since about a week ago I started working from home. I used to work on a powerful iMac at the office but now the only computer that I use for iOS development is my MacBook Air 13” (early 2014). I also have a monitor and a wireless keyboard. My setup looks something like this.

Everything is well and good, my laptop is powerful enough for my programming work. There is only one thing I don’t like about it: its HD is only 120 Gb so it runs out of memory all the time. Or, used to run out of memory until I figured out how to free a lot disk space.

First of all I installed a program called Disk Inventory X, which allowed me to analyse my disk space usage. Then I found out that the most memory consuming thing on my computer was this folder

~/Library/Developer/Xcode/iOS DeviceSupport

It weighs many gigabytes (it weighed tens of gigabytes when I first found it) and they say that it is perfectly safe to delete everything from this folder. Now whenever I feel like I don’t have enough space I clean this folder.

There is also one other folder that rapidly eats up the disk space.


Every time you build an archive in Xcode it ends up in this folder. The archives for some of the apps I work on take more than 100 Mb. Some days I can make quite a few of these archives. Now I remove the older archives from time to time. Just recently I freed 10 Gb on my computer this way.

By the way, the whole purpose of this post is to remind myself of the location of these folders, so I don’t have to look for it in the future.

Another thing I did: I bought a 1 Tb external hard drive. I use it for storing my photos, movies and backing up my documents. I don’t store any photos on my MacBook any more and I even switched off iCloud sync so that the photos I take on my iPhone don’t automatically end up on my computer. I just move my photos from my iPhone to the external drive manually from time to time. 

Now at any time I have at least 20 Gb of free space on my computer which is pretty comfortable for me and I don’t need to buy another computer which is super good.


Creating an email subscription web service (Part 2)

In the previous post of this series I decided what tables I’m going to have in my database. I created those tables (people_table, lists_table, subscriptions_table) using Workbench. Then I had to spend one evening trying to fix an issue with Russian language in MySQL. I just couldn’t save entries to the database, if those entries had Russian symbols in them. I was trying to find a solution that would fix this issue once and for all, like maybe changing some setting in MySQL or in Workbench. But I couldn’t.

So, for now the only way for me to use Russian symbols in my database is to set the collation of each table at the time of its creation to utf8-default. If the table already exists and it has wrong collation, I don’t know how to change it. If you know how to set text encoding globally in MySQL, please, tell me in the comments section below.

I have read something about JavaMail and about setting up a mailing server in general. It all seemed complicated, so I’m not sure if I’ll ever complete this project. I will definitely read some more on this topic, though. For now, I decided to do without the mailing part of an app. What I want to do now, is create a web form with two fields – name and email – and a server side which will get the information from a web form and save it to the database.

So, I created the form. This is what it looks like.


And here is its source code.

Notice the third input – list_id. It has type “hidden”  so it isn’t visible on the page, but the value of the list_id parameter will be sent to the server. This is how the server will know which list the user is subscribing to. The value of this parameter is set manually for now. 

I also created a Web Project in Eclipse called EmailSubscriptionService. To use JDBC in it I downloaded Connector/J from

I needed to put the mysql-connector-java-5.1.39-bin.jar file from the archive somewhere into my project. I didn’t know what the right place for it was, so I just put it into WebContent/WEB-INF/lib folder. I’m almost sure (after googling a little) that it is not the best place for this file. But it works, and that’s what is most important for me right now.

I created two files: and Then I spent two evenings coding. Finally, I have all the main functionality. I can add people to the list from the web form.

This is my file:


And this is file:


This is my lists_table with just one list in it:


After I get the request from the web form I first check if the list with a specified id exists. Just in case.

Then I add a person to the people_table, if that person is not already in the table, which can be checked by the email: each email is unique in the people_table.


After that I get the person_id from the table and add it to the subscriptions_table along with the list_id, which is 1 all the time (it was hardcoded into the web form, as I showed above). This is my subscriptions_table:


What’s next

Since I am more interested in learning databases (and a little bit of web development) than in learning the mailing stuff, I will postpone the learning of JavaMail until better times. Instead, I will create an admin panel for my app. It will allow me to see my lists of subscribers from the browser.

To be continued.