Getting Started with iOS: Objective-C vs. Swift


If you are interested in learning iOS programming and making iPhone/iPad applications, your first step is deciding which language to learn, Objective-C or Swift. In June 2014, Apple announced their new programming language, Swift, at their annual Worldwide Developers Conference. Swift is a language that was created to make it easier for beginners to make iOS applications. They introduced Playgrounds, an added component to Apple’s IDE XCode, which allows you to practice your Swift code without building a whole application. Although Swift is the hot topic among iOS developers, is it better than Objective-C? Objective-C is based on the C programming language which has been around for over 40 years. Since 2008, when Apple first released its iPhone SDK to developers for the App Store, Objective C has been the basis of all applications, frameworks and libraries for programming on Apple products.

What do Objective-C and Swift Look Like?

Let’s take a look at the two languages.

Objective-C is often seen as a barrier to beginners because of its syntax. For example, to initialize an array in Objective-C, you would do the following :

NSMutableArray * array =[[NSMutableArray alloc] init];

But in Swift, it looks like:

var array =[Int]()

What are some differences you see? Does one feel more intuitive than the other?

What about Hello World? Let’s see what printing “Hello World” looks like in both languages:

Objective-C:

NSString*str =@"HelloWorld";

NSLog(@"%@", str)

Swift:

let str ="HelloWorld"

println("\(str)")

You’ll notice that the Swift version has the word “print” and Objective-C doesn’t. Do you see anything else that might make Objective-C a bit more intimidating to a beginner?

Objective-C has very weird syntax with brackets and semi-colons. Swift is a cleaner language with type safe similar to Python. Type safe means you can create variables and not worry too much about their types. This can make things a lot easier , but as a beginner you might want to learn a language that forces you to initialize data with specific types so you can insure you know what you are using and why. The concept of types in programming is a pretty big one, and too big to cover in this post, but you can read more about it here.

What Will I Use Them For?

It’s important to think about what you’re building and why before picking a language. Let’s explore the pros and cons of the two languages based on the context of the apps you’re building.

For work

If you are planning to build a heavy duty application or work for a company that has an application already in the App Store, you will most likely have to learn Objective-C. Because Swift is barely a year old, existing frameworks and applications are almost all built in Objective-C. When modify existing applications or converting them to Swift, you still need to understand Objective-C code.

This is particularly important as you think about your own marketability as a developer. Many mobile companies probably aren’t using Swift yet, while others are making the switch and need more people like you to help them! Learn about the skills that companies you’re interested in are looking for, and consider using this opportunity as a way to deep dive into a language you’d have to use on the job. It’ll make you a stronger developer and a great job candidate.

Another thing to consider is stability. Since Swift is a recently created programming language, there are still many bugs getting fixed by updates. After over 30 years in the market, you will come across less bugs in Objective-C than in Swift. This means that Objective-C is a more stable language compared to Swift. When building your own product from scratch, you might experience lots of bugs when using Swift and you’ll have to constantly change your code to match the new changes in the languages. While the Swift language itself might be easier to get started with, maintaining the code will probably be more work than Objective-C.

For fun

If you are learning for fun and want to build an application that is only available for some of the newest iOS software (iOS 7+), then Swift might be for you. Swift allows beginners to not get tripped up over syntax and more easily focus on building the application itself. Writing in Swift flows so nicely. Even as a beginner, you can produce clean, beautiful code with less effort. I would even say that Swift is more fun. Do you know another language that let’s you use Emojis as variables in your code?

With Playgrounds, you can easily test your code and run it faster. Playgrounds gives you instant feedback on your code, but with Objective-C you can only test your code by running your application. Playgrounds only works with Swift and will immediately show you what your code outputs. The playfulness embedded in the Swift language can make the code easier and more enjoyable to write and understand.

Screen Shot 2015-06-04 at 11.57.28 PM.png

Building an application for fun also means that things like maintainability aren’t as important. Not having users means you can take more risks and try different things without worrying about keeping your users happy. It’s a great chance to learn a new language like Swift that’s easier to pick up, but still has some kinks in it.

emojicodep.png 

Swift not only requires less code than Objective-C, but less files as well. In Objective-C, for each class you create you need header and implementation files. But in Swift, you just need a .swift file. The amount of files you have for one project written in Objective-C adds up. If you create five classes, you’re already at a minimum of 10 files. This can be overwhelming for a new programmer, as all those files need to be maintained and updated as your code changes. Swift having one file for each class makes it easier to organize and maintain your project.

Swift is also faster than Objective-C for certain operations, according to Apple’s release of Swift at WWDC. For example if you have to sort a large data set, according to Apple’s analytics, Swift is faster than Objective-C when it comes to complex sorting. If you aren’t building an application that would perform this type of operation the difference in performance speed between a Swift and Objective-C app is negligible.

swift-performance-language-640x426.jpg

So Which Do You Pick?

There isn’t a definitive answer to this question, but there are different things to consider. It’s important to think about why you’re building your app (for work or for fun), and consider how important things like maintainability, ease of learning, and having fun while coding are to you. Now that we’ve talked about how the two languages hold up on those factors and gave you a peek of what they look like, which are you going to learn?

How Can I Get Started?

So if you’re ready to get started, what tools will you need? For either language, you’ll need a system that is running Mac OSX. The software used to code in Objective-C and Swift is called XCode. XCode is free and available to download in the App Store. If you want to start off with learning tutorials, check out AppCoda and Ray Wenderlich which both have Swift and Objective-C tutorials.