top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to use SWIFT in Objective C Class

0 votes
614 views

I want to know how to make cross bridging to use SWIFT in Objective C class

posted Aug 19, 2014 by Manjunath

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

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!:

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 <Foundation/Foundation.h>

@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.

answer Sep 10, 2014 by Raju
Similar Questions
+2 votes

I am facing situation to use objective c class in SWIFT. So, i am looking for any instruction to achieve this

+1 vote

How to use default values for parameter in function. What will happens when value is not passed for a default parameters.

0 votes

How to declare , set message and show alert in SWIFT.

+1 vote

How to use below code in swift.

[actionBtn addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
0 votes

I am using below code

var i = 1
println(i) //result is 1   
println(%02i)

but its throwing error

...