Swift Features that Differ from Objective-C

Flatiron has decided to focus more on Swift for the latter portion of our program so I decided to look more into this new language that I will be learning for the next few months and beyond. Swift has many new features that I really look forward to embracing and below are a few of them:

Readability

Swift gets rid of Objective-C legacy conventions. What does this mean?

  • No more @ symbols to distinguish between Objective-C type or object-related keywords
  • No more semi-colons when ending lines
  • No parenthesis surrounding conditional expressions inside if/else statements
  • Method calls do not nest inside of each other. Swift methods and functions use industry standard comma-separated list of parameters within parentheses. Below is an example of Objective C:
Source: Computer Science Large Practical: Crash Course in Objective-C Stephen Gilmore School of Informatics

Because of all of these changes, code written in Swift is often shorter, cleaner, and resembles natural English making it easier for programmers to adopt it faster.

Maintenance

Objective-C requires two code files to improve build time, the header and implementation files, which Swift does not require since Xcode and LLVM (Low Level Virtual Machine “a collection of modular and reusable compiler and toolchain technologies”) can infer dependencies and automatically builds.

Type Safety

With type-safety, you’re clear about the types of values in your code to avoid errors such as expecting a String but returning an Int. Swift type checks when compiling your code so you can fix errors early on. Specifying the type is done automatically when assigning the literal value but you need to explicitly specify if a constant or variable. If you assign a value of 100, Swift will infer it to be an Int and if you use assign a value of “Hello Flatiron!”, it will infer a String due to the quotation marks.

Optionals

When calling methods returning nil(uninitialized), nothing happens. This can lead to many bugs. Enter Optionals type, which handles the absence of a value by letting you know that either there is a value and it is x or, there is no value at all.

Playgrounds

This enables one to test out snippets of code without creating a brand new project. While using it, it shows you your expected returns on the right hand side and gives feedback or suggestions when errors arise. I find this extremely useful as it has allowed me to efficiently learn concepts via trial and error. Although it may seem more useful for experienced programmers, it has definitely helped newbies like me grasp simple concepts.