我正在嘗試制作一個 Swift 靜態庫并將其應用于 Swift 和 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)"
}
}
我使用 Swift 撰寫了這樣的代碼。

在 Edit Scheme 選單中,我將 Build Configuration 更改為 Release 并繼續 Run。結果,創建了“libLibrary001.a”檔案和“Library001.swiftmodule”檔案夾。
這兩個工件在粘貼到 Swift 專案中并匯入時效果很好。
但問題是一個Objective-C專案。

我將兩個工件都放入我的專案并檢查:
[一般 - 框架、庫。and Embedded Content] 庫是否被識別
庫是否在 [Build Phases - Link Binary With Libraries] 中被識別
檢查 [Build Settings - Library Search Paths] 地址
定義模塊 - 是
我把'@class Library001_Test;' 在 ViewController.h
#import <UIKit/UIKit.h>
@class Library001_Test;
@interface ViewController : UIViewController
@end
在 ViewController.m 中,加載了 '#import "ProductName-Swift.h" 和創建的類。
#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

錯誤:類訊息的接收器“Library001_Test”是前向宣告
錯誤:接收器型別“Library001_Test”例如訊息是前向宣告
執行此操作時發生錯誤。我已經嘗試了我在互聯網上找到的所有方法,我想知道問題出在哪里。
代碼有問題嗎?我沒有設定好??
在專案中創建的 Swift 檔案在 Objective-C 中很好地匯入......為什么 .a 檔案不能這樣作業?
uj5u.com熱心網友回復:
我的問題是'(ProductName)-Swift.h'
如果你看一下 Swift Code 在 Objective-C 中是如何使用的,很多文章都說 import (ProductName)-Swift.h。所以我只添加了我想要應用的專案頭,但我還需要添加從庫中制作的產品頭。
我的問題很簡單,但我花了很長時間才弄清楚。在 Swift 靜態庫中未找到錯誤“類”和“函式”。我的解決方法是使用我創建的庫的 (LibraryProductName)-Swift.h 而不是您正在處理的專案的 (ProductName)-Swift.h 解決的。
如果您參考下面的地址,您可以防止提前發生的錯誤。
https://medium.com/@mail2ashislaha/swift-objective-c-interoperability-static-libraries-modulemap-etc-39caa77ce1fc
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/463312.html
