The Comparison Between Swift and Objective-C Programming Language

Swift programming language has been announced and released on XCode 6 Beta for over 2 months. Although it is not out of the Beta version yet, we (the iOS mobile Developers) could get our hands dirty on the playing with the Swift code to understand the differences between the Swift and Objective-C Programming Language.

Let’s us make some comparisons between Swift and Objective-C and understand the differences between these 2 programming languages. We can also identify the strengths of Swift over Objective-C.

Swift Programming Language and Its Development

Swift Programming Language is been developed by Chris Lattner and his team since year 2010. He is the director of the Apple Developer Tools such as Xcode, the Instruments and the compiler. As of year 2014, it has been in the development for 5 years.

Steve Jobs passed away on late 2011. The development of Swift has started more than 1 year before Steve Jobs’s passing. It means that Steve Jobs could have involved in part of the Swift Language Development. There is a chance that it is Steve Jobs’s vision that a new and powerful programming language is needed to replace Objective-C (a 30 years old programming language). Only the internal development team could verify if this is a fact or just a random guess.

From watching the WWDC videos on Swift, we understand that Swift is designed to be more
Safe
Modern
Power

The syntax for Swift is designed so that the programmer/developer could make less erroneous codes. When there is any problematic code, the error/warning inside the text editor of the XCode will be much clearer that forces the programmer to correct the mistake. When we program using Swift, we will also write lesser lines of codes when comparing with Objective-C. For example, there is no need of semicolon “;” at the end of each line of code. A few powerful features such as Extensions have been introduced on Swift to ease the developer to create great apps in less time.

The Differences of Swift and Objective-C Code Syntax

Let’s compare the differences between Swift and Objective-C Programming Language. Let’s learn what makes Swift a better programming language.

I have used NSDictionary, NSMutableDictionary, NSArray and NSMutableArray quite extensively on most of my iOS project. So, let’s start to compare these syntax.

Dictionary and Array in Objective-C versis Swift

Objective-C Code

Swift Code

From the above Objective-C and Swift codes are doing exactly the same thing for Dictionary and Array declaration and initialisation. With just the First glance, I believe that you will realize that the code for Swift is simpler and also short.

Check Out the complete documentation for Swift Programming Language: Swift Programming Documentation by Apple.

Swift, Home Automation and Indoor Positioning System

There is not much update on this website on June and July because I have been busy with my company project (Watch Over Me 3.0 Enhancement) and also my personal project. The personal project is a dating social network on mobile. This personal project is developed with the intention to test my skills and to learn new things. I do not have any plan to monetize it at the moment. To develop this app, I am using Facebook and Parse API heavily. The “engine” behind this app is mostly complete. I still have to add some features and polish the interface before ready to be uploaded to the appstore. There will be more updates on this dating social network within the next 2 months.

The Apple Worldwide Developers Conference 2014 (WWDC 2014)

There are some exciting announcements by Apple during the WWDC 2014. For example, the new programming language – Swift and the new frameworks like Home Automation and also the new APIs on CoreBluetooth and CoreLocation. All of these will be ready to ship by Apple probably on September 2014 together with the stable version of XCode 6 and iOS 8.

Swift – Programming Language

Swift is a new programming language that is developed by Apple for iOS and OSX. When the Swift was announced, I did a simple Google search and I was surprised to find out that Swift has been in the development for almost 5 years. It means that Steve Jobs could have been involved in the early stage of the development of Swift. He could have been the visionary behind the development and he knows that Swift could replace Objective-C in the future and make the apps development much easier, faster and powerful.

Since the release of Swift on the XCode 6 Beta, there has been many discussions on the comparison between Swift and Objective-C. At the First glance, I found out that the variable declaration is somewhat similar to JavaScript. After watching the WWDC videos, I understand that Swift is designed to be more safe, more modern and more power.

Safe: The design of the syntax + the auto error detection by the XCode has made the chances of a programmer to create an erroneous code will be reduced to the minimal.
Modern: The programming syntax is modern. It was described as the “Objective-C without the C”. Develop a similar app using both Objective-C and Swift, the development using Swift could be faster because the developer can develop using lesses lines of codes. Some unnecessary syntax such as @ and { } has been removed when comparing with the codes in Objective-C.
Power: Swift has some powerful features such as Nested type and Extensions that will help developer to create great apps.

Here is a list of videos titles related to Swift on WWDC 2014:-
402 Introduction to Swift
403 Intermediate Swift
404 Advanced Swift
406 Integrating Swift with Objective-C
407 Swift interoperability in depth
408 Swift playgrounds
409 Introduction to lldb and the Swift repl
410 Advanced Swift Debugging in lldb

You should be able to watch/download it from: https://developer.apple.com/videos/wwdc/2014/

I have watched all these videos and started to learn Swift since a month ago. I am going to get more serious in it on August and hope that I can master it together with some new features on XCode 6. I am going to convert all the codes on the dating app that I developed to Swift as well.

Apple mentioned that they have big plans for Swift. Well, we shall wait and see.

Home Automation and Indoor Positioning System

Apple has released a new framework calls HomeKit for the iOS developers. The HomeKit can be used to create mobile apps to communicate and control different devices in a house such as television, lightings, air conditioner, garage door and more.

The core technology in the HomeKit is Bluetooth LE. So, the communication using the iPhone/iPad to control the home devices will not consume a lot of battery. Since iBeacon (the indoor proximity system by Apple) is using Bluetooth as well, so, HomeKit and Indoor positioning system are closely related and may compliment each other.

Bluetooth technology is going to play an important role for the years to come as I believe the development of the technology is moving towards indoor. There are many exciting developments in the Smart Home and also Indoor positioning technology. Just to name a few:
1. Google has acquired Nest (maker of “smart” thermostats and smoke alarms for homes) for USD $ 3.2 Billions.
2. The new CoreLocation framework by Apple for iOS 8 will support multistory building. It means that you will know which floor that you are currently standing inside a building from your iPhone.

I am not sure about you. But, I am very excited on the latest development on the mobile app ecosystem. I really can’t wait to see how these new technologies shape the world in the next few years.

iOS Region Monitoring and Location Manager

One interesting feature in the new version of Watch Over Me app is Crime Zone. The main purpose of the crime zones is to notify the user of Watch Over me when he/she is passing the area. Watch Over Me app will ask if the user wants to be watched over during that time.

We have accumulated some trustable and reliable resources from various websites from the Internet to gather the crime zone areas in 7 major cities in 5 different countries (Malaysia, USA, UK, Australia, Thailand). These 7 cities are our test regions and we will be adding more regions in the future.

iOS Region Monitoring and Location Manager

I am using Region Monitoring from the Location Manager to implement this feature on the iOS Watch Over Me app. This particular module is not hard compare with getting the location in iOS 7 in background mode.

The maximum regions that an iOS device can monitor is only limited to 20. So, I have to come out with a good strategy to only get and monitor the regions that are useful to the users.

This is the logic we use for the region monitoring in iOS:-
1. Getting the current location from the user.
2. Send the current location to the server.
3. The server will return all the regions within a 20 km radius from the current location
4. If there are more than 20 regions, the server will only send the closest 20 regions from the current location.
5. After receiving the regions, the app will start to monitor the regions.

A Potential Bug for locationManager delegate method didEnterRegion

After the app has been released on the app store, we are still constantly in testing on the region monitoring on Kuala Lumpur (KL). We have 8 monitored regions in KL at the time of typing this article.

On one fine day, my colleague saw a bug with multiple region notifications triggered at the same time. The location of my colleague is at least 3000 meters away from the nearest region that we monitored. I have double checked on the radius and also the coordinate for all the regions that we monitor in KL, there wasn’t any discrepancy of the data.

Region Monitoring Bug
iOS Region Monitoring Bug
Region MonitoringiOS Region Monitoring


The screen on the Left shows the bug when 4 notifications triggered at the same time when the device is NOT even close to any of the regions.

The screen on the Right is a simple app that I made to stimulate the location of the device (Green Marker) and also the location (Red Marker) and the radius of each region (Red Circle). The Blue Circle is the estimation of the radius of the device when it triggered the notification for all 4 regions. The estimated radius for the device is about 4700 meters! It should not happen.

I have searched through various websites (StackOverFlow, Apple Developer Forum and etc.), I couldn’t find other developer faced the same issue like mine. I saw a lot of questions on NOT getting any notification when passing through a region, but my case is the entire opposite.

I am not sure if this is the fault on my code, on iOS system level or the problem on the local Telco in my country. I have posted a question on StackOverFlow for more than 1 week but I didn’t get any answer as well.

How to Fix this Bug?

Without much reliable information, I can only do trial and error in order to fix this bug. I have been trying a few different method on trying to fix the bug. This includes setting the distanceFilter = 10 for the locationManager. Previously, it was kCLDistanceFilterNone because I want the app to detect every single movement.

My colleague still saw this bug after a few days of testing. So, the above solution is no good.

After experimenting for sometimes, I found a solution that works good.

1. Whenever a user enters a monitored region, he/she will get a notification in the locationManager delegate method didEnterRegion. But due to the bug, this method is not reliable. I have to use a smart way to filter out the fake location.
2. I found a way to get the recent location of the user from locationManager.
3. After the app gets the user location, the app will make sure that the location is within the region before deciding to send the location notification to the device.

From my research, this location is not 100% reliable, especially if there isn’t any location based running at the background. But generally, it is good enough to filter out the bad location which may trigger multiple notifications for different regions.

Here are the example code:

iBeacon and Indoor Positioning System (IPS)

I have been excited for the Indoor Positioning System (IPS) Technology for a quite while. In fact, one of the main reasons I train myself to be an iOS developer is because I want to develop some excited Location and Positioning Based Mobile apps that will change the way we do things in life.

What is Indoor Positioning System?

Indoor Positioning System is a new technology that probably only exists in less than 15 years ago. When I am trying to find more information about this IPS technology from Wikipedia, the earliest research paper on the reference was dated based in year 2009. But when I am searching through google search, I found some research papers on early 2000s.

So, this is a relatively new technology that we haven’t see much applications on the real world yet. But, this will definitely change in the next 5 years.

With the exponential growth of the smart phone users, we have seen a lot of location based (GPS) mobile applications that pop up in the last 5 years. For your information, Global Positioning System (GPS) was developed in 1940s during the World War 2. So, GPS was initially developed to pinpoint the enemy’s location so that they can launch a missile to that location. Thanks to the mobile technology, the GPS technology has been highly commercialised to the mass market in the last 5 years.

All the smart phones users know very well that the Global Positioning System (GPS) does not work well inside a building. This is where the Indoor Positioning System comes into existence.

I am actually betting that the Indoor Positioning System will become highly commercialised within the next 5 years. There will be a lot of innovative mobile apps coming out of it that will make our life easier.

What is iBeacon?

Beacon is actually a device that transmits signal to its surrounding to determine the approximate distance betweens some objects and the broadcaster. We have seen some real world applications like on a car with reverse sensor. That sensor is actually a type of beacon.

iBeacon, on the other way, is a term trademarked by Apple. It is a new iOS technology which is only available on iOS 7 and onwards. iBeacon is using Bluetooth LE (Low Energy) to determine the your location as far as 10 50 meters inside a building.

The vivid location based Mobile apps users know very well that the location based apps usually consume a lot of battery, especially on the navigation mobile apps.

One of the big advantages over the GPS technology is that the IPS technology such as Bluetooth consumes a lot less battery. Even if you turn your bluetooth on 24 hours per day, you almost never feel any battery drain at all.

Some people mentioned that iBeacon is better than the Near Field Communication (NFC) technology that is available on some Android devices. It is due to its greater distance in discovery (iBeacon: 10 50 meters vs. NFC: 20 cms) and energy efficiency.

You may learn the details on the iBeacon here: iBeacon – Wikipedia.

So, What’s Next?

Some people mentioned that after Steve jobs passed aways, there isn’t much innovations coming out from Apple. This isn’t true. We have seen the innovation from Apple from iPhone 5s which has the fingerprint recognition technology and also this iBeacon Technology.

The DNA that Steve Jobs has implemented into Apple will last for a very long time. Apple was late in the map and location technology when comparing with Google. But, it has been catching up fast.

Apple has acquired an Indoor Positioning company – WiFiSlam for $20 millions. There isn’t much news from Apple after the acquisition. I believe we will hear something within the next 1 year.

Don Dodge, who is a Developer Advocate for Google, mentioned in his blog post that there will be many potential markets that could adopt IPS technology. There will be a lot of winners in this technology. He is also an investor and also advisor for some of the IPS startups.

I am very excited to see what will be coming out from iBeacon and Indoor Positioning System within the next 5 years. I just started to learn iBeacon iOS framework and I will make a new post when I am ready to share.

Learning Unit Testing in XCode 5

I have been delaying in learning Unit Testing ever since the released of XCode 5 on September 2013. It is partly due to my busy schedule in rushing 2 complex projects back to back. Now the Second project – Watch Over Me 3.0 is under the beta testing stage, so, I have some time to pick up some skills that I have been wanting to learn.

What is Unit Testing?

Unit Testing is part of Agile Software Development Methodology to make the entire software development process more efficient, the development project to be easier to maintain, less crashes on deployed software and higher quality of code.

Automatic Unit testing, on the other hand, is a proactive way to test a small unit of codes (functions, classes) from time to time to make sure that they are always ready for production. You can host a test server (eg: OS X Server) together with Git repository server. On the test server, some bots will test the latest commits of the codes frequently.

Unit Testing in XCode 5

Apple has done a great job by providing us (The iOS developer) with good framework that can help us to implement unit testing easier on XCode 5. I have learned this unit testing through WWDC 2013 video – Testing in Xcode 5 by Mike Swingler.

From what I learned from the video, we all should start the Unit Testing from Model and Controller. XCode 5 has been designed to make the Testing between the Model and Controller really easy. The new framework for the testing is XCTest.framework. This framework is derived from OCUnit. The iOS developers have been using OCUnit framework prior to XCode5. So, the previous testing codes are compatible with XCTest framework. If you were using OCUnit, you only have to refactor your old code to make it running well with the new framework.

While I was trying to implement the Unit Testing on my old personal Calculator XCode project, I found this simple tutorial (CS305 Software Engineering – Unit Testing in iOS). It shows me some steps and codes using a different calculator project. I learned something new from it.

Be warned, this project is old and it is definitely not optimized for iOS 7.

Example of Unit Testing in Calculator App

I just implemented what I have learned from the video on an old Calculator app project that I have developed for fun some time ago. This is not a usual calculator that most people are familiar. This is a RPN (Reverse Polish notation) calculator.

For example, if you would like to calculate an operation such as 21 + 63, the steps that you have to follow are:-
1. Press 2 and 1
2. Press Enter
3. Press 6 and 3
4. Press Enter
5. Press +
6. Press Enter

You will then get the result showed like the screen below:-
Unit Testing on XCode 5 With Example Project

Unit Testing can be further divided into 2 parts:
1. Logic Unit Testing
– Test only the logic with fixed values and without human interaction
2. Application Unit Testing
– Simulate the user inputs by using user interface such as button and make sure that the app behaves the way it should.

To keep make thing simple, I will only implement Logic Unit Testing for now. I will add more examples when I learn deeper into this Unit Testing.

The Model of my calculator project is CalculatorBrain. It has 3 functions:-
1. pushOperand
2. performOperation
3. popOperand

So, I will perform the unit testing on these functions.

There are only 4 major operations:-
1. Addition (+)
2. Subtraction (-)
3. Production (*)
4. Division (/)

I will implement Logic Unit Testing for all these 4 operations but I will only show an example in this article.

When a user is trying to add 21 with 63, he/she should get the result as 84. Or else, this operation will fail. Here are the steps:-

1. I import CalculatorBrain.h into CalculatorTests.m.
2. I create an instance of CalculatorBrain and instantiate it in setUp method.
3. I create a method calls testAddition.
4. I push 2 numbers (21, 63) using pushOperand.
5. Then, I use performOperation to calculate the result.
6. Lastly, I compare the result with 84 using XCTAssertTrue.

If the logic testing is good, it will show you the following result on the command line:-

If the logic testing is wrong, it will show you something like:

You then will know that you have made a mistake somewhere in the code, you will have to find a way to fix it.

The screen below shows you the complete unit testing for all the 4 operations.
iOS Unit Testing by Example XCode 5

Final Thought

Unit Testing is really an interesting way that helps us to be more proactive in programming. This is especially true when we are able to implement the Bots that help us in testing the project automatically.

You can find the full source code for this simple Unit Testing that I uploaded to Github here: iOS Unit Testing By Using Calculator Project on XCode 5.

Enjoy!

My Experience in Developing WatchOverMe

In this post, I am going to share my Experience in Developing WatchOverMe (WOM). WatchOverMe is a mobile app developed and owned by Secq Me Sdn. Bhd. It is a Location based security Mobile App. You and your loved ones can use the app. You can share your location with your loved ones all the time. In case of an emergency, you can shake you phone and your loved ones will get notified by SMS/email. They will know your exact location and able to take some action in order to help you.

Watch Over Me Mobile App has many different “internal versions“. We call it “internal version” because it is different from the release version. The version for the release version is slightly messed up due to the lack of proper version control in the early stage of development (I haven’t join this company yet).

After I joined the company, I was involved in 3 different version (WOM 2.0, WOM 2.5, WOM 3.0). I involved in improving some features in WOM 2.0 by optimising it to support iOS 7 and also support a new language (Spanish).

I developed WOM 2.5 from scratch for about 2-3 weeks of time and the project has been completed about 70%. But, a decision has been made to cancel this project to push forward for WOM 3.0.

I am also the only iOS developer (Currently based in Singapore) who developed WOM 3.0. Some of the functions in WOM 3.0 are the same with Maxis Circle Watch. But, there are some new functions that are added to enhance the user experience and also user friendliness. WOM 3.0 has a total remade on the user Interface. It is so much polished compare with WOM 2.0.

Here are my experience and thoughts on each of the WOM internal versions.

Watch Over Me Version 2.0 (WOM 2.0)

WOM 2.0 has been there for about 3 years when James (my boss) developed it on his own. This project has at least 2 other programmers before I took over. I only involved in some slight improvements such as adding promotion screen during the year end and also add additional language support – Spanish.

Here are some user interfaces of WOM 2.0.

WOM2.0
WOM2.0
WOM2.0


Watch Over Me Version 2.5 (WOM 2.5)

WOM 2.5 is an improved version of WOM 2.0. There is a total User Interface remade for this app. In terms of functions wise, it shares a lot of similarities with WOM 2.0. But, the base of this app is based on Calendar framework. You can schedule an event using your local calendar on your mobile phone. If there is any event that you will do daily or weekly, you can put that on your calendar. The calendar reminder will prompt the message asking if you want to be watched over about 5 minutes before the event.

WOM2.5
WOM2.5
WOM2.5


From our internal research, we found out that the existing users (including paid users) do not use WatchOverMe as often as they should. So, the calendar reminder is actually very helpful to remind people to use WOM more often.

A decision has been made to cancel this project, so this project never goes live.

Watch Over Me Version 3.0 (WOM 3.0)

WOM is another complex project that I developed from scratch. I did utilise some existing codes from Circle Watch and Existing WOM 2.0. But, due to the the differences in navigation design and others, I still have to make some changes in order to properly place inside the app.

WOM3.0
WOM3.0
WOM3.0


Additional functions for WOM 3.0 are:-

Watch Over Me Event with Photo and Note
When you are running an WOM event, you can take photo and write note on your journey.

Share Journey
When you are running any event, you can share your journey to friends and family members.

Crime Zone (Region Monitoring)
If you have this app installed on your mobile phone and you are driving/walking past a crime zone area, the app will notify you to let you know that you are near a crime zone and ask you if you want to be monitored using the app.

Watch Over Me Event Demo
We have added a polished watch Over Me event demo that teaches the users on how to use the app more effectively.

Event history
You are able to look back all the historical events that you have launched so far.

Report Crime
You may report crime area with photo and information. The information will be kept in the server database and shared with other users of the app.

Map with Route
When you are running a Watch Over Me event, you will know your exact location on the map every minute. You see the path that you have been driving/walking.

WOM3.0
WOM3.0
WOM3.0


As of 18 March 2014, this project has been completed about 85-90%. We will deploy the app to the beta testers in a few days. The estimated date to launch this app is on late March.

WOM3.0
WOM3.0
WOM3.0


You may download Watch Over Me (iOS) From: Watch Over Me (iOS).

Here is the demonstration of the WOM 3.0 with XCode and Simulator