objective-cswiftobjective-c-category

How to use Objective-C category in Swift file


I have an Objective-C project, there is a file named 'NSDictionary+Test', and a method named - (void)testDone:(TestBlock)block in the file. I add a swift file to the project , how can i use - (void)testDone:(TestBlock)block in my Swift file.


Solution

  • So firstly you have your Objective C proj and in it you have your category(in Obj C) and now you added a Swift file and want to use category method in Swift.

    So now answer goes-

    1. Firstly add your .swift file and mostly it asks for creating a bridging header. If it does not get created, follow the steps from this.

    2. Add #import "NSDictionary+Test.h" before #endif.

    3. Now you should be able to use the (void)testDone:(TestBlock)block as dictionary.testDone(block : TestBlock!).