How To Use Objective-C Classess in iOS Swift
You might need to use of the objective-C classes in Swift. This is possible. Just do the following:
- Add .m (Objective-C implementation)
This file, that is, a .m one, should be added to the class and named CustomObject.m - Add the Bridging Header
During the addition of the .m file, a dialog will appear which looks as follows: Just click on Yes.
- Add .h, the Objective-C Header
Add another .h. Give it the name CustomObject.h - Build the Objective-C class
In CustomObject.h, this should be as follows:
12345#import <Foundation/Foundation.h>@interface CustomObject : NSObject@property (strong, nonatomic) id aProperty;- (void) aMethod;@end
In CustomObject.m, t should be as follows:
123456#import "CustomObject.h"@implementation CustomObject : NSObject- (void) aMethod {NSLog(@"aMethod Ran");}@end - Add a class to the Bridging-Header This should be:
1#import "CustomObject.h" - Use your Object
1234var iOfObject: CustomObject = CustomObject()iOfObject.aProperty = "Hello World"println(iOfObject.aProperty)iOfObject.aMethod()
The bridging header is so useful since with it, there is no need to import explicitly. You are done, so you can safely use the classes.