0

Simple Scenario: I want to call any Swift Package member function from within Objective-C.

I'm building a reference/demo/proof-of-concept app to mix ObjC with Swift.

Here's a working snippet from the Swift side:

import Foundation
import RicPackage

final class Greetings: NSObject {
    @objc public static let shared = Greetings()
    private override init() {}
    
    @objc public func doSomething(name: String) {
        print("Do something, \(name)")
        let ricClass = RicClass()
        ricClass.sayHello()
    }
    
    @objc public func hello() {
        print("Hello Everybody!")
    }
}

Here's the Objective-C side:

#import "ViewController.h"
#import "ObjcSwiftHeader-Swift.h"

@class RicClass;

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"Inside VC ViewDidLoad.");
    Greetings *greetings = [Greetings shared];
    [greetings hello];
    [greetings doSomethingWithName: @"Ric"];
    
    RicClass *ricClass = [RicClass alloc];
}


@end

I got this compiler error:

enter image description here

Here's the snapshot of the project:

enter image description here

Here's the Swift Package stuff:

import Foundation

public struct RicStruct {
    public private(set) var text = "Hello, World!"

    public init() {
    }
    
    public func sayHello() -> String {
        "Hello Ric!"
    }
}

public class RicClass: NSObject {
    @objc public var msg = "Mother has a feeling, I might be too appealing."
    @objc public let text = "Hello Everybody!"
    
    public override init() {}
  
    public func sayHello() {
            print(text)
        }
    
    public func doSomething() {
        print("Inside doSomething()")
    }
}

There must be a simple solution....

Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105
  • Have you reviewed the documentation? https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_swift_into_objective-c – Scott Thompson Sep 23 '21 at 21:32
  • I can't get it to work for accessing an imported Swift Package vs a member .swift file. – Frederick C. Lee Sep 23 '21 at 22:01
  • I changed the code per apple doc and got a compiler error (above). – Frederick C. Lee Sep 23 '21 at 22:19
  • Forward references should be *only* in headers. In implementation files you need the import with the definition (that is, the Swift header). Also, the syntax to create an instance in Obj-C is `[[myClass alloc] init]`. – Sulthan Sep 23 '21 at 22:28
  • I know about the 'init'. I merely focussed on the error. The 'init' was immaterial. I've imported the swift header. – Frederick C. Lee Sep 23 '21 at 22:49
  • Positioning the @class doesn't alter the result. Same error. I think the easiest approach is merely access the Swift Package via a Swift interface with ObjC vs directly from ObjC. This effort isn't worth the hassle. – Frederick C. Lee Sep 23 '21 at 22:55

1 Answers1

1

Packages are linked as dynamic embedded frameworks into your app and to use them you should import a package's library to your source file:

Swift:

import RicPackage

Objective-C:

@import RicPackage;
iUrii
  • 11,742
  • 1
  • 33
  • 48