DEV Community


Obj-C — Swift — Obj-C

itscoderslife profile image Coder Originally published at on ・2 min read

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 #import typedef enum : NSUInteger { male, female } Gender; @interface Person : NSObject @property (nonatomic, strong) NSString \*name; @property (nonatomic, strong) Gender \*gender; @property (nonatomic, strong) NSUInteger \*age; - (NSString\*)formattedName; @end // Objective-C Implementation of Person class @implementation Person - (NSString\*)formattedName { return [NSString stringWithFormat:"This is %@",]; } @end
Enter fullscreen mode Exit fullscreen mode

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 \(" : "This is \( aged \(self.age) years." return formattedString } }
Enter fullscreen mode Exit fullscreen mode

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!

Discussion (0)

Editor guide