Is there any way to put Objective-C classes within Swift projects?
Is there any way to put Objective-C classes within Swift projects?
First create a new "Cocoa Touch Class" in File > New > File
Put Objective-C as a language:
AfterclickingtocontinueXCodewillaskifyouwanttocreateaBridgingHeader(it'stheclassthatwillbridgethetwolanguagesbyimportingtheObjective-CcodeintoSwift).
Clicktocreate:
Inthisclassimportthe.hoftheclassyouwanttouseintheswiftproject,inthiscaseIimportedtheclass"Import"
#import "Import.h"
In class Import was created only one method to print a string
//Código do Import.h
#import
@interface Import : NSObject
-(void) printName:(NSString*)name;
@end
//Código do Import.m
#import "Import.h"
@implementation Import
-(void) printName:(NSString*)name{
NSLog(@"%@", name);
}
@end
Now, just instantiate the class from any part of the swift code and call its method, for example in the viewDidLoad of the ViewController:
override func viewDidLoad() {
super.viewDidLoad()
var i = Import()
i.printName("Maria")
}