How to call Objective-C code from Swift?

Tutorial on how to use Objective-C Classes in Swift and visa versa
06 September 2017   13561

Let's consider two different ways of calling Obj-C code from Swift.

Using Objective-C Classes in Swift

Step 1: Add Objective-C Implementation -- .m

Add a .m file to your class, and name it CustomObject.m.

Step 2: Add Bridging Header

When adding your .m file, you'll receive a promt with Yes, No and Cancel buttons. Hit Yes.

Step 3: Add Objective-C Header -- .h

Add another .h file and name it CustomObject.h.

Step 4: Build your Objective-C Class

In CustomObject.h

#import <Foundation/Foundation.h>

@interface CustomObject : NSObject

@property (strong, nonatomic) id someProperty;

- (void) someMethod;

@end

In CustomObject.m

#import "CustomObject.h"

@implementation CustomObject 

- (void) someMethod {
    NSLog(@"SomeMethod Ran");
}

@end

Step 5: Add Class to Bridging-Header

In YourProject-Bridging-Header.h

#import "CustomObject.h"

Step 6: Use your Object

In SomeSwiftFile.swift:

var instanceOfCustomObject: CustomObject = CustomObject()
instanceOfCustomObject.someProperty = "Hello World"
println(instanceOfCustomObject.someProperty)
instanceOfCustomObject.someMethod()

There is no need to import explicitly; that's what the bridging header is for.

Using Swift Classes in Objective-C

Now, let's go visa versa and learn how to use Swift Classes in Objective-C.

Step 1: Create New Swift Class

Add a .swift file to your project, and name it MySwiftObject.swift.

In MySwiftObject.swift:

import Foundation

class MySwiftObject : NSObject {

    var someProperty: AnyObject = "Some Initializer Val"

    init() {}

    func someFunction(someArg:AnyObject) -> String {
        var returnVal = "You sent me \(someArg)"
        return returnVal
    }   
}

Step 2: Import Swift Files to ObjC Class

In SomeRandomClass.m:

#import "<#YourProjectName#>-Swift.h"

The file:<#YourProjectName#>-Swift.h should already be created automatically in your project, even if you can not see it.

Step 3: Use your class

MySwiftObject * myOb = [MySwiftObject new];
NSLog(@"MyOb.someProperty: %@", myOb.someProperty);
myOb.someProperty = @"Hello World";
NSLog(@"MyOb.someProperty: %@", myOb.someProperty);
NSString * retString = [myOb someFunction:@"Arg"];
NSLog(@"RetString: %@", retString);

Apple to Relaese Swift 5

Official builds are prepared for Linux (Ubuntu 14.04, 16.04, 18.04) and macOS (Xcode)
27 March 2019   307

After a year of development, Apple released the update of the Swift v. 5. Official builds are prepared for Linux (Ubuntu 14.04, 16.04, 18.04) and macOS (Xcode). The source code is distributed under the Apache 2.0 license.

The new version has ABI stabilized for macOS, iOS, tvOS and watchOS platforms. It is possible to deliver new versions of libraries without recompiling applications. In the standard library, the internal representation of strings is translated to UTF-8 encoding. Improved support for raw text (with special characters and line breaks) in string literals. New type for Result error handlers and SIMD vector type added. The possibilities for interpolating strings with the String type (text selection from arbitrary data) are expanded. The performance of the Dictionary and Set types has been increased.

The package manager provides support for mirroring dependencies, associating assembly parameters with a target platform, generating coverage data, and defining custom requirements for supported target platforms. The ability to import libraries into the REPL without assembly in the format of executable files has been added to the "swift run" command.

Get more info at official website