Monday, 3 November 2014

iOS8 How to use Objective-C Classes in Swift

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.
If you’re wanting to know more about the iOS 8 Programming language Swift, then check out the following articles.


Apple has 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. In some cases, I had to add an explicit to an older Objective-C File:

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

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.


Enjoyed this post?

No comments:

Post a Comment