What? Apple has brought out swift to start programming iOS 8 Applications. What if I need to use Objective-C in Swift? Don’t worry. You can do this.

Apple have some really good documentation here . So, If you have an existing class that you’d like to use, perform Step 2 and then skip to Step 5. For some cases, I had to add an explicit to an older Objective-C File:


1
#import <Foundation/Foundation.h>

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 likely be hit with a prompt that looks like the one below, Click Yes!:

iOS 8 Bridging Objective-C with Swift

If you did not see the prompt, or accidentally deleted your bridging header, add a new .h file to your project and name it <#YourProjectName>-Bridging-Header.h

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


1
2
3
4
5
6
7
8
9
#import <Foundation/Foundation.h>

@interface CustomObject : NSObject

@property (strong, nonatomic) id someProperty;

- (void) someMethod;

@end

In CustomObject.m


1
2
3
4
5
6
7
8
9
#import "CustomObject.h"

@implementation CustomObject : NSObject

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

@end

Step 5: Add Class to Bridging-Header

In YourProject-Bridging-Header.h:


1
#import "CustomObject.h"

Step 6: Use your Object

In SomeSwiftFile.swift:


1
2
3
4
var instanceOfCustomObject: CustomObject = CustomObject()
instanceOfCustomObject.someProperty = "Hello World"
println(instanceOfCustomObject.someProperty)
instanceOfCustomObject.someMethod()

No need to import explicitly, that’s what the bridging header is for.

Using Swift Classes in Objective-C

Follow these steps:

Step 1: Create New Swift Class

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

In MySwiftObject.swift:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 Objective-C Class

In SomeRandomClass.m:


1
#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


1
2
3
4
5
6
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);

Note: CodeCompletion wasn’t behaving as accurately as I’d like it to. On my system, running a quick build w/ “cmd + r” seemed to help Swift find some of the Objc code and vice versa.

This was a great article on StackOverflow.

Enjoyed this post?

Subscribe to our RSS Feed or Follow us on twitter to keep up to date with the latest from iOS-Blog. Remember, Sharing is caring so please click one of the following options:

Tags: , , ,