Enterprise Mobility Blogs

perspectives for IT decision makers

Using Objective C and Swift, together – Developer Concerns

by (October 8, 2015)

Swift and Objective C

Swift, which was launched at WWDC 2014, is expected to eventually, replace Objective-C as the main language to develop iOS and MAC applications. Being a new language, it has its own unique features and is also, able to adapt the best features and syntaxes from other existing languages.

Objective C has been the main language since the development of iOS applications.  With the introduction of Swift, Apple got aware of the requirement of interoperability of both the languages. This was necessary, as the core libraries are all in Objective C and porting all of those to a new language would be a lot of work. Objective C developers needed a learning curve and thus, usage of Swift and Objective C in a single project is made possible and easy to implement. Codes could be called from Swift files to Objective C files and vice versa.

When developing a Cocoa application, there are three aspects of this compatibility which turn out to be advantages. They are:

  • Interoperability – Allows you to interface between Objective C and Swift code. It lets you use Swift classes in Objective-C and makes use of the original Cocoa practices and patterns.
  • Mix and match – Mixed-language applications, containing both language files, are allowed and can communicate with each other.
  • Migration – Options to migrate from existing Objective-C code to Swift, with the help of the above features, have been made simple and fast. Latest Swift features can be used to replace parts of the existing code in Objective C.

The details of how to achieve the mixed-language codebase is provided in this document. Click here.

10 Concerns Objective-C Developers face while using Swift

  1. Learning curve – For a new language, the learning curve for the developers is common. Objective C developers will find the syntax of Swift as new, since Objective C has a unique syntax structure compared to other languages.  You should not  hesitate and should start programming. The knowledge of Objective-C patterns and system frameworks in Cocoa are relevant to Swift.  Therefore, Objective-C developers should consider themselves, already, proficient in Swift.
  1. Optionals – One of the main features in Swift is the optionals and its unwrapping. Though it might take some time to get used to optionals and its syntax, optionals helps you to be clear about the values that your code can work with and avoid missing the nil-check. It allows you to discover the potential issues at compile time rather than runtime. Hence, optionals is of great advantage and widely, appreciated.
  1. Constantly evolving – It is a fact that Swift is constantly evolving. The language has already gone through a few major changes from the day it was released and upgraded to 2.0. Syntax has changed, methods renamed and features added. This has led the developers to go back to code, written with Swift 1.0, and put efforts to make it work with the new changes. Though the changes are often trivial, it still requires spending some time to bring back your code base into line, after a new Xcode and Swift version are released.
  1. Less availability of Swift source libraries – Most of the open source libraries and sdk’s, available, were built on pure objective C. For example, the AFNetworking, Facebook, Google Plus So, at the initial phase of the project, you are forced to write some wrapper classes which communicate from our swift classes to those libraries.
  1. A single project can have both Swift and Objective-C together, and call Objective-C code from Swift, and vice versa, with ease. However, the objective C code cannot use all the new features of Swift, such as Swift struct and enum Thus, until the full porting of the application to Swift is not done, it won’t be able to make full use of Swift’s features.
  1. Subclass limitation  Sub classing an Objective class from a swift class is not yet possible. Therefore, converting a large iOS project in Objective C to Swift is considered to be a huge task, as the class you migrate, cannot have any Objective-C subclasses in your application. The most effective approach for migrating code to Swift is on a per file basis—that is, one class at a time.
  1. typedef NS_ENUM( ) You should use NS_ENUM macro to make your enums Swift-compatible.

typedef enum {

FruitApple = 1,
FruitBanana = 2,
} Fruit;

will have to be written as the following to be used in Swift codes.

typedef NS_ENUM(NSUInteger, Fruit ) {

FruitApple = 1,

FruitBanana = 2,

};

  1. No Preprocessor: A preprocessor is not included in the Swift compiler. It takes advantage of compile-time attributes, build configurations, and language features to accomplish the same functionality. Thus, the preprocessor directives are not available in Swift.
  1. No complex Macros possible: It was a common practice to use complex macros in C and Objective-C to avoid retyping large amounts of boilerplate code. eg. #define DLog(fmt, …) NSLog(@”__PRETTY_FUNCTION__ %s”, fmt)

However, macros can make debugging and refactoring difficult. For Swift, this can be achieved using functions and generics without any compromises. Therefore, the complex macros   possible Objective-C files are not present in Swift coding.

  1. Memory side effects – Though the ARC is present in Swift, but Swift is strongly typed and typed safe, so all variables must have a known type and non-nil value (unless declared optional). This will compel developers, who don’t like to use the optionals, to add some hacks that have nothing to do with the actual application code.   Outlets are weak, by default, because the outlets you create, usually, have weak relationships.

Comparison Quick Reference Chart

Objective C vs. Swift

Conclusion

Since, the Swift is evolving continuously and swiftly, it is a proven fact that Apple foresees it as a major language for the iOS application development.

It is going to be a slow-but-steady conversion from Obj-C to Swift and therefore, developers, having knowledge in both, will be in demand. But looking at the pace of Swift in gaining popularity, adapting to the change is likely to happen.

Swift functions have a lot of options and a lot of power and with great power comes great responsibilities.  The power has been given to you. Now, it’s your turn to use it wisely.

By,

Sudeep KS

iOS and Swift Development Expert, RapidValue Solutions

Sorry no comment yet.

LEAVE A COMMENT

24/7 Toll Free(877)-643-1850(US)

Would you like to know more about us?

  • This field is for validation purposes and should be left unchanged.
Scroll Top