Swift
ViewController.swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.swiftFunction()
print(self.swiftFunctionWithReturnValue())
self.swiftFunctionPassingValueToObjectiveCClass()
self.callOjectiveC()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
//Dispose of any resources that can be recreated.
}
func callOjectiveC()
{
let objClass:ObjectiveCClass = ObjectiveCClass()
objClass.callASwiftFunction()
}
func swiftFunction()
{
//Using objective c function here
print(“Swift Class (__FUNCTION__)”)
let objClass:ObjectiveCClass = ObjectiveCClass()
objClass.objectiveCfunction()
}
func swiftFunctionWithReturnValue()->String
{
var returnValue:String;
let objClass:ObjectiveCClass = ObjectiveCClass()
let objCReturn:String = objClass.objectiveCFunctionWithReturnValue()
returnValue = “Returning from swiftFunctionWithReturnValue , appending (objCReturn) “
return returnValue
}
func swiftFunctionPassingValueToObjectiveCClass()
{
let objClass:ObjectiveCClass = ObjectiveCClass()
objClass.objectiveCfunctionWithValuePassedFromSwiftClass(“Passing a string from Swift Class”);
}
func functionFromObjectiveC()
{
print(“In swift class , Function from objective c”);
}
}
ObjectiveCClass.h
#import <Foundation/Foundation.h>
@interface ObjectiveCClass : NSObject
– (void)objectiveCfunction;
– (NSString *)objectiveCFunctionWithReturnValue;
– (void)objectiveCfunctionWithValuePassedFromSwiftClass:(NSString *)passedValue;
– (void)callASwiftFunction;
@end
ObjectiveCClass.m
#import “ObjectiveCClass.h”
#import “ObjectiveCSwiftBridging-Swift.h”
@implementation ObjectiveCClass
– (void)objectiveCfunction
{
NSLog(@”Calling %s”,__func__);
}
– (NSString *)objectiveCFunctionWithReturnValue
{
return @”Returning value from Objective C Class objectiveCFunctionWithReturnValue function”;
}
– (void)objectiveCfunctionWithValuePassedFromSwiftClass:(NSString *)passedValue
{
NSLog(@”%@ and printed in objective c class %s”,passedValue,__func__);
}
– (void)callASwiftFunction
{
ViewController *swiftViewcontroller = [[ViewController alloc] init];
[swiftViewcontroller functionFromObjectiveC];
}
@end
Click here to download Bridging beetween Swift and Objective C Project