UncleJunior
UncleJunior

Reputation: 241

Export Swift framework for the usage in Objective-C

I am creating a framework using Swift to be used in my college's Objective-C Project. I have created a new project and have chosen Cocoa Touch Framework and named it printFramework. I have added a Swift file into the project with the code below:

import Foundation


@objc public class printClass : NSObject {
    @objc public override init() {
        print("My name is init")
    }

    @objc public func printMe(){
        print("Hello Wrold")
    }
}

I build the above code (Build Success) with the target Generic iOS Device, and now I have a briefcase (framework) file and under Product, and I create another Single View Application project with language Objective-C named prinFrameTest. I copied the framework into the project and in General tab under Linked Frameworks and Libraries, I have my printFramwork and above that in Embedded Binaries I have added my printFramework.

In my ViewController.m I have the following code:

#import "ViewController.h"
@import printFramework;
#import <printFramework/printFramework-Swift.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    printClass *pr;
    [pr printMe];
}

@end

I build and run this (it ran successfully), Simulator opens and but the code is not working (means it does not print anything in the log).

What is going wrong?

Upvotes: 2

Views: 865

Answers (3)

Quang Vĩnh H&#224;
Quang Vĩnh H&#224;

Reputation: 514

  1. You need to use [[YourClass alloc] init] to make it work
  2. in Xcode project, you need to enable Always embed Swift standard libraries
  3. Building with Generic iOS Device does not produce a universal framework

Upvotes: 1

Jogendar Choudhary
Jogendar Choudhary

Reputation: 3494

If you put a breakpoint on the line [pr printMe] and inspect the value of pr, you'll probably see that it's a null reference - you haven't created the object, you've just allocated a variable which can hold a reference to an instance of printClass. Try this way:

[[printClass new] printMe];

Upvotes: 1

Shehata Gamal
Shehata Gamal

Reputation: 100523

You need to create a non nil object either

printClass *pr = [printClass new];

OR

printClass *pr = [[printClass alloc] init];

Upvotes: 0

Related Questions