Over the years I have literally taught tens of thousands of people how to write code in Objective-C through training classes, conferences, online forums, and my book series. Based on the mountain of feedback I have received, I can tell you some key points that make Swift much easier to learn than Objective-C.
If Objective-C is the only language you have ever coded in, I have one thing to say about moving to Swift. Welcome to the 21st century.
I have coded for many years in other more modern languages such as Java and C#, so when I added Objective-C to my repertoire in 2008, there were a lot of things I missed from the other languages I work in. Fortunately, I'm getting most of that back with Swift.
1. Classes are Defined in a Single File
In Objective-C, classes are defined in two files—a header (.h) file and an implementation (.m) file. This is a real pain in the neck, and one of the biggest complaints I hear from developers moving to Objective-C.
In Swift, your classes are defined in a single .swift file. Thank you Apple.
2. No More Square Brackets for Messages
The square brackets used to pass messages to an object are the second biggest complaint I have received about Objective-C. It's unlike other more modern languages and it really throws a curve to people new to Objective-C.
In Swift, you use dot notation to call a method on an object, similar to the notation you use when accessing a property. For example:
This code calls the calculator object's addToTotal method, passing 14 as the value to be added.
3. The End of the Asterisk
I can't tell you how many times I have had to answer the question of why some variables in Objective-C have asterisks associated with them and others don't (variables that hold a pointer to an object have an asterisk).
In Swift, a variable can contain a pointer to an address in memory (although not a direct pointer), but you don't ever need to use an asterisk in this context. I'm weeping openly.
4. No More Messages to Null Objects
In Objective-C you can declare a variable that is designed to hold a reference to an object. However, if you forget to create an instance of an object and store it in this variable, there was no compiler or run-time error generated because Objective-C allows you to send messages to null objects. For example:
I've heard all the arguments about why this feature of Objective-C is great, but from a practical perspective, for the majority of the code that you write, this is a bad thing because when you do this unwittingly, it causes something to not work properly in your app and you don't know why.
Enter Swift.
Swift doesn't allow you to create properties or variables that are not initialized. If you forget to initialize a variable you will get a compiler error and you will know immediately what you did wrong rather than spending several minutes stepping through the debugger.
The more errors that are caught at compile time, the easier your app development will be. Period.
5. Importing Just Got Easier
Importing header files in Objective-C is cumbersome. If you want to work with a class from within another class file you have to import its header file first.
In Swift, all classes you created in a single project can automatically be accessed without using an import statement.
6. Alloc and Init are No More!
In Objective-C you create an object from a class like this:
In Swift, you create an object from a class like this:
Much easier! I'm going to plan an extra day of vacation this summer for the extra time I'll be saving by not typing alloc and init.
8. No More Rockets Blowing Up!
A number of years ago, one of the space agencies was launching a rocket that self-destructed not long after takeoff. They discovered that the problem was caused by a programmer who left a break out of a switch statement. Ouch.
In Objective-C, if you accidentally leave out a break in a switch statement, control automatically flows down to the next case in the switch. This is rarely desired behavior and can wreak havoc in your app.
In Swift, you don't need to add a break statement to each case in a switch because after a case executes, the switch is automatically exited. What's also nice about Swift switch statements is that you can test for just about anything including strings! Here is an example from Apple's documentation:
No break statements, hence no bugs caused by leaving them out!
8. Easier String Manipulation
In Swift, working with strings is much easier and far more obvious to programmers new to the language:
- Strings don't need an @ in front of them as they do in Objective-C.
- You can combine strings together by using the + operator.
- You can check if two strings contain the same characters by using the = operator
These behaviors are all expected and make string manipulation and comparison far easier.
Conclusion
This list is really just a small subset of the more obvious things to love about Swift. In future posts I'll go into more detail and show you the basic syntax and mechanics of Swift.