Objective-C within Swift?

0

Is there any way to put Objective-C classes within Swift projects?

    
asked by anonymous 02.05.2018 / 19:49

1 answer

0

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")
}
    
30.07.2018 / 23:08