Home > Back-end >  Receiver '**' for class message is a forward declaration Error. Swift Static Library use i
Receiver '**' for class message is a forward declaration Error. Swift Static Library use i

Time:04-24

I am trying to make a Swift Static library and apply it to Swift and Objective Project.

import Foundation

@objc open class Library001_Test: NSObject {
   public override init(){}

   @objc public func testPrint() {
      print("My Name is Andi")
   }

   @objc public func getUUID(userName: String) -> String {
      let uuid = UUID().uuidString
      return "\(userName)'s UUID : \(uuid)"
   }
}

I wrote the code like this using Swift.

enter image description here

And in the Edit Scheme menu, I changed the Build Configuration to Release and proceeded with Run. As a result, the 'libLibrary001.a' file and the 'Library001.swiftmodule' folder were created.

enter image description here These two artifacts work well when pasted into a Swift project and imported.

But the problem is an Objective-C project.

enter image description here

I put both artifacts into my project and checked:

  1. [General - Frameworks, Libraries. and Embedded Content] whether the library is recognized

  2. Whether the library is recognized in [Build Phases - Link Binary With Libraries]

  3. Check [Build Settings - Library Search Paths] address

  4. Defines Module - Yes

And I put '@class Library001_Test;' in ViewController.h

#import <UIKit/UIKit.h>

@class Library001_Test;

@interface ViewController : UIViewController
@end

And in ViewController.m, '#import "ProductName-Swift.h" and the created Class were loaded.

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

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    Library001_Test *test = [[Library001_Test alloc] init];
}
@end

enter image description here

error : Receiver 'Library001_Test' for class message is a forward declaration

error : Receiver type 'Library001_Test' for instance message is a forward declaration

An error occurred while doing this. I've tried all the methods I've found on the internet and I'm wondering where the problem is.

Is the code the problem? Did I not set it up well??

The Swift file created in the project is import well in Objective-C... Why the hell is the .a file not working like this?

CodePudding user response:

My problem was with '(ProductName)-Swift.h'

If you look at how Swift Code is used in Objective-C, many articles say to import (ProductName)-Swift.h. So I only added the project header that I want to apply, but I also need to add the product header made from the library.

My problem was simple, but it took me a long time to figure it out. The error was not found 'Class' and 'func' in Swift static library. My workaround was resolved using the (LibraryProductName)-Swift.h of the library I created, rather than the (ProductName)-Swift.h of the project you are working on.

If you refer to the address below, you can prevent the error that occurred in advance.

https://medium.com/@mail2ashislaha/swift-objective-c-interoperability-static-libraries-modulemap-etc-39caa77ce1fc

  • Related