Swift for Beginners

Last week I published a little essay about Swift, imploring iOS developers to start learning Swift today:

The number one question I hear from iOS developers when Swift comes up is “Should you switch to Swift?” and my answer for that is “Probably yes.” It’s of course not a black and white answer and depends on your situation, but if you’re an experienced Objective C programmer, now is a great time to start working in Swift.

In last week’s iOS Dev Weekly Dave Verwer astutely pointed out, however, this advice is really aimed at experienced iOS developers:

Jason talks mainly about experienced iOS developers in this article but I believe it’s a whole different argument for those who are just getting started.

The intersection of programming languages and learning programming happens to be precisely my line of work, so I thought I’d offer some advice for beginner iOS developers and Swift.

Should you learn Swift or Objective C?

For newcomers to the iOS development platform, I reckon the number one question asked is “Should I learn with Objective C or with Swift?” Contrary to what some may say (e.g., Big Nerd Ranch), I suggest you start learning with Swift first.

The main reason for this argument is cruft: Swift doesn’t have much and Objective C has a whole bunch. Swift’s syntax is much cleaner than Objective C which means a beginner won’t get bogged down with unnecessary details that would otherwise trip them up (e.g., header files, pointer syntax, etc.).

I used to teach a beginners iOS Development course and while most learners could grasp the core concepts easily, they were often tripped up by the implementation details of Objective C oozing out the seams. “Do I need a semicolon here? Why do I need to copy and paste this method declaration? Why don’t ints need a pointer star? Why do strings need an @ sign?” The list goes on.

When you’re learning a new platform and a new language, you have enough of an uphill battle without having to deal with the problems of a 1980s programming language.

In place of Objective C’s header files, importing, and declaring of methods, Swift has just one file with a single declaration and implementation of methods, with no need to import files within the same module. There goes all that complexity right out the window. In place of Objective C’s pointer syntax, in Swift both reference and value types use the same syntax.

Learning Xcode and Cocoa and iOS development all at once is a monumental task, but if you learn it with Swift first you’ll have a much easier time taking it all in.

Swift is ultimately a bigger language than Objective C, with features like advanced enums, Generics/Templates, tuples, operator overloading, etc. There is more Swift to learn but Cocoa was written in Objective C and it doesn’t make use of these features, so they’re not as essential for doing iOS development today. It’s likely that in the coming years Cocoa will adopt more Swift language features, so it’s still good to be familiar with them, but the fact is learning a core amount of Swift is much more straightforward than learning a core amount of Objective C.

A Note about Learning Programming with Swift

I should point out I’m not necessarily advocating for learning Swift as your first programming language, however, but instead suggesting if you’re a developer who’s new to iOS development, you should start with Swift.

If you’re new to programming, there are many better languages for learning, like Lisp, Logo, or Ruby to name just a few. You may very well be able to cut your teeth learning programming with Swift, but it’s not designed as a learning language and has a programming mental model of the “you are a programmer managing computer resources” kind.

Learning Objective C

You should start out learning iOS development with Swift, but once you become comfortable, you should learn Objective C too.

Objective C has been the programming language for iOS since its inception, so there’s lots of it out there in the real world, including books, blog posts, and other projects and frameworks. It’s important to know how to read and write Objective C, but the good news is once you’ve become decent with Swift, programming with Objective C isn’t much of a stretch.

Although their syntaxes differ in some superficial ways, the kind of code you write is largely the same between the two. -viewDidLoad and viewDidLoad() may be implemented in different syntaxes, but what you’re trying to accomplish is basically the same in either case.

The difficult part about learning Objective C after learning Swift, then, is not learning Cocoa and its concepts but instead the earlier mentioned syntactic salt that comes with the language. Because you already know a bit about view controllers and gesture recognizers, you’ll have a much easier time figuring out the oddities of a less modern syntax than you would have if you tried to learn them both at the same time. It’s much easier to adapt this way.

Learn Swift

Perhaps the biggest endorsement for learning Swift comes from Apple:

Swift is a successor to the C and Objective-C languages.

It doesn’t get much clearer than that.

Speed of Light