Swift Weekly – Issue 05 – The Builder Pattern and Fluent Interface

A few weeks ago I started checking out some Wikipedia articles about various s/e design patterns and came across theBuilder pattern which is a Creational GoF pattern. Then as you know, I cannot just read one article in one sitting. I have to click every link that the article leads to, so I stumbled upon the article about Fluent Interfaces and I could then see the possibilities.

Note: Fluent Interfaces have nothing to do with IB or a visual interface that is displayed on the screen at all. Fluent interfaces are the way that we can write our software to ensure they are… well… fluent. Read on to understand how this works.

I don’t think fluent interfaces are the same as the builder pattern. I don’t really think fluent interface is actually a pattern at all. I believe that fluent interfaces are a concept, and a kick ass one at that. I think mixing fluent interfaces and the builder pattern will allos us to build Swift classes that are amazingly simple to use, instead of the classic OOP designs that we see on pretty much every Apple class these days. I wish Apple could read this article and (ehem), just update their iOS SDK classes for instance to use fluent interfaces and the builder pattern.

If you want to write your Swift apps in the most kick ass way, continue reading. I think this article will help you a lot not only in learning more about Swift, but also writing some really crazy code that will make your life and those around you much easier.

Click here to read the full article on GitHub.

Creating string enumerations in Objective-C (The ultimate solution)

A while ago I wrote on my blog about a solution to one of the most common questions asked by Objective-C programmers which is “How can I create string enumerations?”. Well, the solution that I’ve given has immediately become one of the top subjects that attracts developers to my blog, as I can see in my stats. I thought I should now take it to a whole other level and get rid of the limitations that I had presented in the old solution, and come up with a fresh perspective.

The following video is the result of my work on this subject. I hope you’ll enjoy watching it.

Getting the most out of your WiFi dongle data plan (The definitive guide)

I am currently facing a situation where I need to carry out my work while I am connected to a WiFi dongle. I have spent about £20 for 6GB of data that can be used for up to 3 months, whichever comes first. So I have now learnt a lot of tricks on how to minimize my data usage and save money. Here, I want to share with you some of these tricks.

Trick 1 – Disabled images in your browser

If you view the web without images, your browser does not have to load those images from the internet so you will save A LOT of data. Every browser is different so I cannot tell you how to do that on every browser. Here is how to disable the loading of images on Safari on OS X:

  1. Go to Safari Preferences
  2. Go to the Advance tab
  3. At the bottom of the Advance tab, find the “Show Develop menu in menu bar”
  4. Then close the preferences window
  5. Go to the newly-shown Develop menu and choose Disable Images

That’s it. Some websites will not look nice without images and that just shows you how some web developers have now become too reliant on using images! Their fault really.

Trick 2 – Use Git commands carefully

I use Git through the terminal and two of the most useful commands that I use quite often are git fetch and git rebase. Now, git fetch, will fetch every new branch and all change-sets from the server and brings it into your local machine. What you want to do is to replace git fetch with the following command:

git fetch origin NAME OF BRANCH

So if you are on a branch named “ios7” and you want to just bring the remote changes on that branch to your local repo, do this:

git fetch origin ios7

git rebase origin/ios7

Now you got only the changes in that branch and you rebased to the latest changes

Trick 3 – Use Adblock… A LOT

Install Adblock on your browser and go to your popular websites and start blocking anything that looks useless and will consume your data. So just start blocking all content that is irrelevant.

Trick 4 – Disable Flash

I don’t have to explain this. Flash videos are memory, CPU and data hungry. They consume your battery so fast it is unbelievable. Just disable Flash to make sure your browser will not try to load those darn Flash videos for you.

Trick 5 – Stop watching videos on websites

Do not go to YouTube and other streaming video websites. They are very data hungry no matter how low you have set the quality of the videos. Watch videos when you are on a proper broadband, not on a tiny WiFi dongle.

Trick 6 – Subscribe to websites with push notifications

I read MacRumors quite a lot. I go to their website probably 3-4 times a day. But instead of doing that, it’s best that you subscribe to push notifications from news websites that offer that service. Then you can get push messages directly to your OS X desktop of new content that get posted on those websites instead of going there every time you want to check the news. A push notification will only contain the new article, not the whole website. But if you go and browse the website, you are viewing everything that is on the front page at least, hopefully without the images, as one of the tricks you learnt earlier!

Trick 7 – Stop opening a browser page ever time you think you’ve lost your internet connection

We all do this. When we think we have no internet connection, which can happen on a daily commute on a Wifi dongle, we open our browsers and navigate to our favorite website. If the website loads, we go “Aha, it works”. Instead of that, do this:

  1. Open Terminal
  2. Type this: ping http://www.apple.com

Once you see ICMP packets going and coming back, you know at least ICMP is working and you are almost 99.99% sure that your internet works.

Trick 8 – Do not preload top hits in your browser

Users of Safari or most modern browsers know that when they type in the search field for anything, a few “suggested” websites and terms will appear automatically. For instance, if you start typing “blood”, in your search field in your browser, you may get suggested words such as “blood pressure”, “check your blood sugar level”, and etc. Every time you type something in your browser’s search field, your browser will hit your search engine and look for suggested words and links. This will take battery and will use your internet. To disable this in Safari, follow these steps:

  1. Open Safari Preferences
  2. Go to the Privacy tab
  3. In the “Smart search field” area, click the “Prevent search engine from providing suggestions”
  4. In the same area, click the “Do not preload Top Hit in the background”

Okay that is it for now. These are all the tricks I have up my sleeves for saving you some data usage on your WiFi dongle. If you have any other tips that you believe are worth sharing with others, please let me know and I will include them here.

Enjoy

Learn iOS programming

Giving private and group iOS development lessons

Hello internet. After writing many many books and developing iOS apps for 6+ years, I feel I am now ready to take on a new challenge. To start teaching iOS development to people who are interested in transforming or perfecting their career.

I am available for teaching iOS development to you ANYWHERE in the world you are in. If you are in London or in Brighton in the UK, we can have face to face lessons. If anywhere else in the world, I can teach you over Skype.

Classes will be tailor made to suite your free times, abilities, etc. If you are interested, get in touch by sending me an email at: vandad.np@gmail.com

 

Jenkins doesn’t load under OS X Mountain Lion (or Server)? Here is the fix!

Answer: your machine doesn’t have Java installed on it. This happened to me. Jenkins installed fine, but then when I opened it on its default port of 8080, nothing happened.

To fix it, open Terminal and type “java” without the quotation marks.

This will tell you that you have to have Java SE installed on your computer and then will download and install Java SE automatically for you. Done! 🙂

String enums in Objective-C

Note: I have written a newer and better solution to this problem in a new video, which you can watch by clicking here.

Sooo a lot of programmers think enumeration items cannot be strings, and they are right, BUT, there is a BUT. You have to understand that C strings that are made out of 4 characters, each of which is 1 byte long, constitute a memory address that is 4 bytes long, or just the equivalent of int, NSInteger or whatever you want to call it.

So if you put a value such as ‘Good’ for the enumeration item, in fact, the compiler will translate the values of ‘G’, ‘o’, ‘o’ and ‘d’ as their numerical values and will generate a hexadecimal value and put it as the integral value of the enumeration item.

Let’s say we want to say “Good”, or “Nice” all in enumeration items in C. Here is the example, this code runs fine in Objective-C for iOS or OS X as well:

Screen Shot 2013-03-24 at 19.30.24

The output of this is the string “dooG” printed to the screen if you run this on an iPhone device for instance. The reason is the bytes are obviously reversed as the string ‘Good’ has the letter ‘d’ as the lowest byte and that ends up being at the first byte of the string so we end up with “dooG”. That’s not good though, is it? So we just have to swap the bytes around to get the proper string:

Screen Shot 2013-03-24 at 19.31.08

Easy peasy, aye? The highlighted line is very important. That line swaps the order of bytes in the integer. Also note that I am using the calloc function as it will not only allocate the memory but also set the byte values to 0x00 in the memory for us so we don’t have to do it manually or with another procedure.

xcrun is an absolute mess

So I just ran this command:

xcrun -sdk iphoneos –run PackageApplication -v “build/Release-iphoneos/testing.app” -o “/users/vandadnp/Desktop/” -s “iPhone Distribution: Pixolity Ltd.” –embed “/Users/vandadnp/Library/MobileDevice/Provisioning Profiles/Pixolity_Wildcard_Ad_Hoc.mobileprovision”

To sign a simple app and package it into an .ipa file and guess what? The packaging failed and xcrun DELETED MY WHOLE DESKTOP. You know what that means? That means all my development environment is wiped without me being able to get them back. This is absolutely mad.

So xcrun failed to do the .ipa file and then tried to clean up after itself except for the fact that the cleaning up happened on my desktop. And what is Xcrun’s solution to cleaning up? Of course, it deletes the whole freaking destination folder, in this case, my desktop.

Apple, big fail, seriously, you guys need to educate yourselves on how to create IDEs and development tools.

Setting and Achieving Goals

What are Goals?

Before you begin, there are a few things I need to ask you to do:

  • You need pen and paper if you want to really learn something from this article. You will just waste your time if you are reading it because you are bored. Just stop and save yourself some time.
  • You need to physically write down what I ask you to write down. Do not type them on a computer or any other electronic device. Physical contact with a pen and writing your goals down on a piece of paper is scientifically proven to have a better effect on the results that you achieve. Do not fall into the trap of memorizing your goals or writing them in a word document.
  • Write them down on a piece of paper and have that paper always with you to remind yourself of your goals. If you write your goals down on paper that you have no access to during your work hours, then this exercise is pretty much useless.

I have been obsessed with setting and achieving goals. Ever since I was a teenager, about 16 or 17 years of age, I was setting daily goals for myself. I will later mention how setting daily goals is counterproductive so bear with me while I explain all these.

I started setting goals for myself when I was young, to utilize my time and focus my energy on things that I believed at that time were important to my growth. Let me give you an example: I have always been a geek. One of the ultimate projects a computer geek would like to work on, would be writing his or her own operating system. So I set on the challenge of programming an operating system. I was setting daily goals for myself that would ultimately result in what I understood at that point in time to be the perfect program. I followed my dream through to the point that I actually had a working operating system but then later I gave up. Other important things happened in my life. I left the operating system and never went back to it. What happened there? Why did I just leave it without ever going back to it? Was I discouraged by an external factor? Was I overwhelmed or perhaps was the task too much for a young man like me at that age to solve? I am yet to find the answer to some of these questions but I have, over the years, come to conclusion as to why we humans like setting goals and why we sometimes fall short of following them through until we get results.

We all like checklists. We like to have a list of things that we need to take care of every day. The daily goals that we set and accomplish every day will really shape our lives. They will shape who we are, what we say and what we do. Goals are indeed powerful tools either to make us or destroy us, depending on how we set them, what we set them to and whether or not we can and will follow them.

Sometimes we even set goals to fool ourselves into believing that we are busy! I remember setting daily goals for myself years ago and then after a few months I noticed that I didn’t really accomplish much towards the bigger goals that I had in mind. You see, goals have an extreme and unique power. They have the power to pull us towards them. What happens if you get pulled towards your daily goals? Well, the answer is simple. You will become your daily goals. Now what happens if we set weekly goals? Is that any different from daily goals? How about monthly or even yearly goals? Are those any different from daily or weekly goals?

Let’s take an athlete as an example. When a road cyclist trains for the olympics, she will set a target weight for herself, amount of calories to consume and burn on a daily basis, a list of foods that she can eat and a list of food that she must not; she will even adjust her sleeping hours to make sure that she is getting enough sleep. All this hard work for what? To get to the olympics and make her country proud. Well, wait a second. What happened there? How is that an athlete can follow through such a long-period of intense goal setting and exercising and dieting but we cannot even set daily goals for ourselves and achieve them? What is so special about the way the athlete was setting goals for herself and why don’t we possess the same goal-setting skills?

Goals are very powerful, as I mentioned before. They are the mechanism through which we can achieve our dreams or go as far as destroying our future. It depends on us and it all comes down to how we set goals. In next sections of this article I will describe to you a method that I have perfected over the years that may or may not help you not only set, but also follow through and achieve your goals.

Setting Goals

In this part of the article, I will describe to you the method that I personally use to set goals for myself. I have used various methods, either self made or the ones that I have learnt from others. The method I’m going to discuss here is a mix of everything I have learnt so far about setting and following through goals. I have broken it down into smaller chunks and steps so that they are easier to follow.

1 – Start With an Empty Mind

The first thing that you need to do is to just empty your mind. Got issues at work? Forget them. Now is not the time to think about that. Do you have issues in your marriage or relationship? Throw them out the door. Empty your mind. You need to make sure that no external (emotional) factor is predetermining the output of this exercise. Once you are completely relaxed and your mind is at east, then you can go to the next step.

2 – Write Down 4 Things That You Want to Achieve in The Next 4 Years

Your goals all have to have the following qualities:

  1. They must be bold and brave, with no limitations in mind
  2. They must be very specific. Vagueness only discourages you and may/will affect your overall performance towards achieving that goal
  3. They must be written down on a piece of paper, not just memorized in your head

Studies have shown that human beings can only really remember 4 things at a time easily. Once there is more than 4, you will start to forget one or the other. So set 4 main goals for the next 4 years. Make sure that you are pushing yourself and setting really big, yet achievable, goals. For instance, saying “Go skydiving” may be a good goal but it’s not good enough. Going skydiving requires some money… and that’s it! So all you are saying here is “Make £200 money and register for skydiving”. That’s not a big enough goal to achieve in 4 years. The very important part about this step is this: do not limit your thoughts. Have you been wanting to achieve something but you’ve never been focusing enough on it? Now is the time. Maybe set that as one of your yearly goals. For instance, you may say “Get the mortgage sorted in the next 4 years and become a house owner”. This can be a great goal to set for the next 4 years.

Another example of a good goal would be to say, for example, “Double my income from x to y”. So you see, doubling an income is something that people will look at as a big, yet achievable goal. So I say: go for it, set big goals for the next 4 years. There is one thing that I need to tell you about though. Unachievable goals only discourage you from setting further goals. For instance, I am a computer engineer. What if I set a goal that said “Build a spacecraft and fly to the moon”. Even though this is “achievable” by definition, it most definitely not achievable by me alone. So this is a silly goal to set and in the long run, will only discourage me as I spend more and more time and energy on it and basically waste my life. So although I am asking you to set big goals, I want to make sure that you are not setting out-of-this-world goals that are not achievable.

3 – Write Down 4 Goals That You Want to Achieve in Each Year, For The Next 4 Years

The previous step was about setting 4 goals that you want to achieve in the next 4 years. This step however is about setting 4 goals for every one of the 4 years

For instance, my Step 2 goals can be written down like this:

My goals for the next 4 years:

  1. Buy a house
  2. Start a family
  3. Travel across Europe
  4. Travel to America

Now that I know the most important goals of the next 4 years of my life, I have to break it down into smaller details. So I will then start to describe what I have to achieve each year, for the next 4 years, in order to achieve my bigger goals, mentioned before:

  • Year 1
    • Save £40k for the down-payment of the mortgage
    • Take my relationship with Ms X to the next level and see if there is a spark
    • Start saving up for EU travel next year
    • Start planning for the US travel
  • Year 2
    • Goal 1
    • Goal 2
    • Goal 3
    • Goal 4
  • Year 3
    • Goal 1
    • Goal 2
    • Goal 3
    • Goal 4
  • Year 4
    • Goal 1
    • Goal 2
    • Goal 3
    • Goal 4

The important thing to keep in mind is this: the yearly goals that you set have to be directly connected to the 4-yearly goals that you have set for yourself. For instance, in the previous example we set the goal of buying a house, starting a family, traveling across EU and America. Now, when it comes down to setting goals for each year, I cannot ignore my 4-yearly goals and go on a drunken episode of setting goals that are completely irrelevant to my 4-yearly goals. Make sure they are relevant to the 4-yearly goals, that’s what I’m trying to say here!

4 – Set 4 Goals For Every Month of Every Year of Your 4-Year Plan

I think now you are getting the idea. Now depending on each year’s goals, break the yearly goals down into smaller chunks that can fit into 12 months. For instance, if one of your goals 4-yearly goals is to buy a house, and one of your first year’s goals is to save £40k money, then you can set the goal of saving £3.33k every month in the first year to achieve that goal. Now how achievable that is, is down to you to decide. I am not saying you should do this. I’m just giving you an example.

As you start setting monthly and weekly and daily goals, you will notice that the task becomes more and more cumbersome and time consuming. You will sometimes not know how you can set the monthly or weekly/daily goals. In that case, just leave the paper blank and move on to the next month/week/day. It’s absolutely ok. Do not beat yourself over because you cannot write goals like a machine. We are humans, we have limits and this is one of them as you probably have noticed by now. Make sure the paper on which you write your goals is by your side, always! This way, once you do think of a goal, you can write it down immediately. If you don’t have the paper with you, just forget it. The moment is gone. You won’t be able to capture that goal unless you keep it in your head until you get hold of your paper. Forget about capturing them electronically on your smartphone. It won’t work, I can guarantee. Once a goal is written in electronic format, you probably won’t ever bother achieving it. It needs to be written in your handwriting, on a piece of paper, using a pen that you physically hold in your hand.

Make sure that you write all these down. If you are just reading this article without writing these down, stop! Stop right now and get a pen and paper.

5 – Set 4 Goals For Every Week of Every Month of Every Year of Your 4-Year Plan

Write 4 goals for ever week. The goals in every week have to be directly related to the goals that you have set for that particular month.

6 – Set 4 Goals For Ever Day of Every Week of Every Month of Every Year of Your 4-Year Plan

I don’t have to explain this anymore, do I? Great.

You may be tempted to set more goals for every day and that is ok. You can do that if you want to! But the 4 goals that I’m asking you to set every day is the absolutely necessary and the most important goals that you need to achieve every day.

 7 – Go to Bed And Leave Your Written Goals for a Whole Week, Revise After a Week

This perhaps is one of the most important steps that a lot of us just never bother with. If you are like me, you can probably set all these goals in a day but how achievable are these goals? How likely are you to actually want to achieve them? These are two different matters. Something that matters to you today may not matter to you the next. That is why you have to constantly review and amend your goals.

Achieving Goals

You have your goals written down on a piece of paper. But is that all you need to achieve your true potential and goals? Nope! I am sure you could see that coming. That’s correct. You need much more than just a list of goals. You need to do the followings to be able to achieve your goals:

  1. Review and amend your goals on a weekly basis, preferably on Sunday, mid-day is my preference. Sunday usually works out best but you can find another day if it works better for you.
  2. Review your last week’s performance and list 4 things that you learnt from last week and how these 4 things can help you in your revisions of your goals for the next 4 years. If you learnt a valuable lesson, make sure that you amend your list of goals if it applies.
  3. If a goal is not relevant anymore, don’t keep it in your list. Get rid of it. For instance, if one of your current-year’s goals was to “Double my salary” and by now you have tripled your salary, probably there is no point to keep the original goal in your list unless you want to double your tripled salary, in which case, I say, go for it. Do it. Why not?

There is one important thing that I’ve noticed as well during setting and achieving goals. You will need more time for yourself than you have been allocating in the past. That means your goal setting may or may not affect your relationship with people around you. Mastering this art takes time and after you have mastered it, you no longer will see this affecting your relationships. Just bear in mind that if you start losing friends because you have been so focused on your goals, you have to ask yourself “Was it worth losing this person?”. If the answer is yes, then carry on. But if you held that person very dear, then revise your plans and priorities. You may also have to do some damage control. Talk to the person, get her/him back if they are worth your life/time.

I hope these guides will help you. I know that by now you probably have a lot of questions that I have not answered. That’s what the comments box below is for. Just leave some comments/questions. Show some love. I will make sure I answer your questions. Also if a friend/family can benefit from this article, please share it with them.

Building and Running Python Scripts with Xcode 4.x

 Edit 1 (20th October 2014): Xcode 6.1 instructions can be found here: https://vandadnp.wordpress.com/2014/10/20/building-and-running-python-scripts-with-xcode-6-1/

So you have installed Python and you want to use Xcode to write and run your Python scripts… is that correct? Ok then, follow these steps to create a simple project and run it with Python using Xcode 4.x…

  • Open Xcode and select File->New->Project…
  • From the left hand side, under the OS X category, select Other
  • Now on the right hand side, select External Build System and press the Next button, as shown below

Click to Enlarge

  • Now open a Terminal window and type which python in it, and keep the results in mind or just keep Terminal open for future reference, as shown below. This will tell you where Python is installed on your system.

Click to Enlarge

  • As you can see, in my case Python is installed under /usr/bin/Python
  • Now back to Xcode, set the following values on the second screen of project creation:
    • Product Name: test
    • Organization Name: company
    • Company Identifier: com.company
    • Build Tool: enter the path to Python which we retrieved from Terminal. I have entered /usr/bin/Python (see below)
  • Once you are done entering all four values, press the Next button.

Click to Enlarge

  • Once you pressed Next, now you should choose where you want to save your project, save it somewhere that makes sense for you. I am going to save it in ~/Desktop/Development/Projects/Python/Sandbox/test/ as that’s how I have structured my development environment. Once you are happy with the folder that you are saving your project in, press the Create button in Xcode’s dialog, as shown below

Click to Enlarge

  • Now select the “test” target from the breadcrumb menu on top left corner of Xcode’s main window and choose Edit Scheme, as shown below

Click to Enlarge

  • In the Edit Scheme screen, on the left hand side, choose the Run option. Then on the right hand side, choose the Executable section and press the Other… item. This will open an Open dialog for you asking you to choose the binary of Python. For me, Python was sitting in /user/bin/python as you saw earlier. So in Mac, I press Cmd+Shift+G to “Go to Folder” and then I type in /usr/bin/, as shown below and then press Go:

Click to Enlarge

  • Now that you are in the folder that contains the Python binary, find and pick the binary and press the Choose button
  • Back in Xcode, on the same Scheme window that we were on before, under the Debugger section under Info, make sure that you choose None. This is very important.

Click to Enlarge

  • Back in Xcode, on the same Scheme window that we were on before, navigate on the right hand side to the Arguments tab and under the Arguments Passed On Launch, press the + button and type test.py, as shown below:

Click to Enlarge

  • Now go to the Options tab, under the Working Directory, click the Use custom working directory and then put the full path to where you are intending to save your Python files in this project. My project is at ~/Desktop/Development/Projects/Python/Sandbox/test/ and I’m intending to save my first file as ~/Desktop/Development/Projects/Python/Sandbox/test/test.py so I will choose ~/Desktop/Development/Projects/Python/Sandbox/test/ as my working directory, as shown below:

Click to Enlarge

  • We are now done with the Schemes. Press the OK button to close this window.\
  • In Xcode, from the Menus, choose File->New->File…
  • From the left hand side of the dialog, under the OS X section, choose Other and then on the right hand side, choose Empty and press the Next button, as shown below:

Click to Enlarge

  • In the next screen, make sure that you set the value of Save As as test.py. This is what we have been preparing our Scheme for, that we are going to have a file called test.py that needs to be passed to the Python interpreter. Have a look at the following screenshot. Once you are done, press the Create button:

Click to Enlarge

  • Now enter the following code in your test.py file: print “It works!”, as shown below:

Click to Enlarge

  • As the final step to make sure everything is working fine, in Xcode, from the menus, choose Product->Run, the Debugging Area will popup and show you the output of the print command like so:

Click to Enlarge

Lovely, it’s all working. I hope this tutorial helped you. If you have any questions or suggestions, please leave them in the comment sections down below…