Posts

Showing posts with the label objc

Use Swift Class in Objective C

Image
Sometimes we would like to include the Code written in the Swift Language to be used in Objective-C file. Recently, I ran into a situation where I need to use my Swift written code into Objective-C .m file. Before we start this, we need to understand @objc . It's an attribute which helps the swift code interoperability with Objective-C code. Consider the below code: class User : NSObject {     init ( _ nameId: Int ) { }     init ( _ name: String ) { } } Here, the 'User' can be initialized by nameId or name, but Objective-C does not understand this short hand implementation. It would not know which initialization to call , one with nameId or one with just  name. class User : NSObject {     @objc(initWithNameID:) init ( _ nameId: Int ) { }     @objc(initWithName:) init ( _ name: String ) { } } The Objective-c code will now know there are two initialization methods, one initWithNameID  ...