How to use Swift code in Objective C class


Screen Shot 2015-04-24 at 5.03.18 PM

Now you can easily use swift code in your objective C class.

Step 1. Go to packaging in Build setting and get Product Module Name. Change it or write any other name. see attached image.

Step 2. Go to Object c class in which you want to use Swift class. and import class as following.

#import “msy-Swift.h”

Where msy – Product Module Name

You don’t need to import all swift class .this line import all swift classes in your project in your class.

Step 3::

Assume your swift class method is


class func commonAlert(title:String,msg:String,curView:UIViewController){

        var device : UIDevice = UIDevice.currentDevice();

        var systemVersion = device.systemVersion;

        var iosVerion : Float = (systemVersion as NSString).floatValue

        if(iosVerion >= 8.0) {

    

        var alert = UIAlertController(title: title, message: msg, preferredStyle: UIAlertControllerStyle.Alert)

            alert.addAction(UIAlertAction(title: “Ok”, style: UIAlertActionStyle.Default, handler: nil))

                  // return alert

            curView.presentViewController(alert, animated: true, completion: nil)

        }else{

            let alert=UIAlertView(title: title, message: msg, delegate: self, cancelButtonTitle: “ok”)

            alert.show()

           

        }

    }


You can call this func using your objective c code like following

[CommonClass commonAlert:@”Hi ” msg:@”How to use Swift code in Objective C class” curView:self];

Best Of Luck

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: