Objective C vs Swift

  1. Swift is easier to read
  2. Apple’s Swift language is a compiled language type.
  •       Objective-C is a language built on C. To differentiate keywords and types from C types, Objective-C introduced new keywords using the @ symbol. Because Swift isn’t built on C, it can unify all the keywords and remove the numerous @ symbols in front of every Objective-C type or object-related keyword.
  • Swift drops legacy conventions. Thus, you no longer need semicolons to end lines or parenthesis to surround conditional expressions inside if/else statements.
  • Another change is that method calls do not nest inside each other resulting in brackets i.e [[[ ]]]. Method and function calls in Swift use the industry-standard comma-separated list of parameters within parentheses.

Ex:

In Objective C:

               [[self tableView:tableView cellForRowAtIndexPath:indexPath] isSelected];

               In Swift:

if let cell = tableView.cellForRow(at: IndexPath(row: n, section: 0)), cell.isSelected {
    //Everything ok
}

2. Swift is easier to maintain

  • Legacy is what holds Objective-C back—the language cannot evolve without C evolving. C requires programmers to maintain two code files in order to improve the build time and efficiency of the executable app creation, a requirement that carries over to Objective-C.                                                                                                                                                    Swift doesn’t have two file requirement as Xcode & LLVM compiler can figure out dependencies. Swift combines the Objective-C header(.h) and implementation files (.m) into a single code file (.swift). The two file system of Objective-C requires additional work that complicates the app development process.

3. Swift is safer

One interesting aspect of Objective-C is the way in which pointers—particularly nil (null) pointers—are handled. In Objective-C, nothing happens if you try to call a method with a pointer variable that is nil (uninitialized). The expression or line of code becomes a no-operation (no-op), and while it might seem beneficial that it doesn’t crash, it has been a huge source of bugs. A no-op leads to unpredictable behavior, which is the enemy of programmers trying to find and fix a random crash or stop erratic behavior.

Optional types make the possibility of a nil optional value very clear in Swift code, which means it can generate a compiler error. This creates a short feedback loop and allows programmers to code with intention. Problems can be fixed as code is written, which greatly reduces the amount of time that you will spend on fixing bugs related to pointer logic from Objective-C.

Traditionally, in Objective-C, if a value was returned from a method, it was the programmer’s responsibility to document the behavior of the pointer variable returned (using comments and method-naming conventions). In Swift, the optional types and value types make it explicitly clear in the method definition if the value exists or if it has the potential to be optional (that is, the value may exist or it may be nil).

To provide predictable behavior Swift triggers a runtime crash if a nil optional variable is used. This crash provides consistent behavior, which eases the bug-fixing process because it forces the programmer to fix the issue right away. The Swift runtime crash will stop on the line of code where a nil optional variable has been used. This means the bug will be fixed sooner or avoided entirely in Swift code.

4. Swift is unified with Memory Management

Swift support for Automatic Reference Counting (ARC) is complete across the procedural and object-oriented code paths. In Objective-C, ARC is supported within the Cocoa APIs and object-oriented code. It isn’t available for procedural C code and APIs like Core Graphics. This means it becomes the programmer’s responsibility to handle memory management when working with the Core Graphics APIs and other low-level APIs available on iOS. The huge memory leaks that a programmer can have in Objective-C are impossible in Swift.

A programmer should not have to think about memory for every object created. Because ARC handles all memory management at compile time.

5. Swift requires less code

In Objective-C, working with text strings is very verbose and requires many steps to combine two pieces of information. Swift adopts modern programming language features like adding two strings together with a “+” operator, which is missing in Objective-C.

Leave a comment