Show Menu

Do you have a job opening?

Submit your 30 day Job Listing for FREE

If you do not know, and by now you should. Apple have created a whole new programming language called Swift. Do not fret you Objective-C Coders out there. This tutorial will show you how you can use Objective-C Classes in Swift.

We have an updated article on Swift Classes for Swift 2, If you’re interested.

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:


#import  

Step 1: Add Objective-C Implementation — .m

Add a .m file to your class, and name it CustomObject.m. If you need information on starting an iOS Application check out our: Create new Single View Application

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

For an explanation of how to create an Objective-C Bridging Header. Check out: How to create an Objective-C Bridging header

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


#import 

@interface CustomObject : NSObject

@property (strong, nonatomic) id someProperty;

- (void) someMethod;

@end

In CustomObject.m


#import "CustomObject.h"

@implementation CustomObject : NSObject 

- (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()

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:


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:


#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);

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.

having issues?

We have a Questions and Answer section where you can ask your iOS Development questions to thousands of iOS Developers.

Ask Question

FREE Download!

Get your FREE Swift 2 Cheat Sheet and quick reference guide PDF download when you sign up to SwiftMonthly


Sharing is caring

If you enjoyed this tutorial, please help us and others by sharing using one of the social media buttons below.


iOS-Blog Admin Team

Written by:

We're here to help.

Comments

There are currently: 2 Responses to “How to use Objective-C Classes in Swift”. Leave your comment!

May be you can record video tutorial?
I really can’t understand Step#1. When I drag&drop .m file from finder to xcode swift project, I don’t see any bridging request.


    Maybe it doesn’t work with drag and drop. It works if you create a new file (but only the first time, if for some reason you delete the bridge file and add another Swift file, currently the dialog doesn’t appear again).


Leave a Reply

Would you like to sign up to the mailing list by our sister site: SwiftMonthly?