Obj-C — Swift — Obj-C

This is one of the rare scenarios one might get into, but the problem and its solution are unique and the simplest one here.

  • Have you come across a scenario where you felt the need for an additional method in a class inside a framework (one of the Pods you added) to achieve your work?
  • Is the library implemented in Objective-C?
  • Is your legacy code in Objective-C and now you are strategically moving to Swift?

Today at work my friend, Vinodh, asked me this:

“I have a pod framework which is in Objective-C, I need to add a functionality but needs to be used in Obj-C for now and may be used in Swift file in future. What is the best way to do this?

Let us say we have a class called Person inside one of your pod frameworks. This Person class has name, age and gender as properties, with a function to format the name.

// Objective-C header file of Person class

typedef enum : NSUInteger {
} Gender;

@interface Person : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) Gender *gender;
@property (nonatomic, strong) NSUInteger *age;

- (NSString*)formattedName;


// Objective-C Implementation of Person class
@implementation Person

- (NSString*)formattedName
    return [NSString stringWithFormat:"This is %@",self.name];


When the formattedTitle method is called on a person object with value for name property ‘Vinodh’ it would print like “This is Vinodh”.

Now we need to extend this Person class with another method say a new format which tells the age along with the name.

In classic Objective-C style it would be adding a category to Person class, Importing this category wherever you need this new method. This would have been the best way before Swift was introduced.

But there is a disadvantage if new developer join unitl and unless (s)he imports the category header, code completion will not show the new method. Probably the new developer will end up writing the same method creating a new category again. This is a problem.

Swift to rescue. Do the following:

  • Create a Person class extension and write the needed method
  • Now create a protocol and declare the method signature
  • Now prefix the protocol with @objc to make this available in Obj-C files

Here’s how you do it.

@objc protocol Formats {
    func getFormattedName(with ageHidden: Boolean) -> String

extension Person: Formats {
    func formattedName(with ageHidden: Boolean) -> String {
        let formattedString: String = ageHidden ? "This is \(self.name)" : "This is \(self.name) aged \(self.age) years."
        return formattedString

Now wherever you type [personObject form… in Obj-C or in personObject.form… in Swift the code complete will show you both the methods one Person class another from the extension we added.

Love to hear more stories from your side. Please do share in comments.

Happy Coding!


Compiler directives @ objective-C 2.0

Here are the list of directives in Objective-C 2.0. Let me know if anything is missing.

// directives used in class declaration
@interface @public @package @protected @private @property @end

// directives used in class implementation
@implementation @synthesize @dynamic @end

// creating a string object

// for forward declaration

// similar to function pointers

// used for method declaration mainly for delegation purpose
@protocol @required @optional @end

// to assure thread safety

// handling memory issues (useful in ARC enabled apps)

// throwing and handling exceptions
@throw @try @catch @finally

// type encoding (Apple docs)

// aliasing an existing class name to a new one (useful in refactoring)

Happy coding!