我知道有很多其他的帖子都是关于这个问题的,但我唯一能找到的和我的(Objc Bridge)有相同背景的帖子是this...但这对我来说并不管用.

我有以下层次 struct :

// Dependency chain: 
// AppTarget->Lib1
// Lib1->Lib2

AppTarget
Lib1
  -- AppDelegate.swift
Lib2
  -- ObjCppWrapperApple.mm (contains ObjCppWrapper ObjC class)
  -- ObjCppWrapperIOS.mm (Contains a category named 'iOS' for the ObjCppWrapper ObjC class)

// Note: The bridging header is the same for Lib1 and Lib2. Both ObjCppWrapperApple.hpp and ObjCppWrapperIOS.hpp are included in the bridging header

ObjCppWrapperApple.hpp:

@interface ObjCppWrapper : NSObject

// Some methods

@end

ObjCppWrapperIOS.hpp:

#import     "ObjCppWrapperApple.hpp"

@interface  ObjCppWrapper (iOS)

+ (BOOL) PreInitialize NS_SWIFT_NAME (PreInitialize());

// Other methods

@end

ObjCppWrapperIOS.mm:


#import "ObjCppWrapperIOS.hpp"

@implementation ObjCppWrapper (iOS)

+ (BOOL) PreInitialize        
{
    // Some code
}

// Other methods

@end

从AppDelegate.swft中,我调用

ObjCppWrapper.PreInitialize()

而这在以下方面失败了

Thread 1: "+[ObjCppWrapper PreInitialize]: unrecognized selector sent to class"

而这只发生在tvOS上.它为iOS工作.

根据this stackoverflow post,类别和原来的班级应该在同一个目标.这是我的情况,但从Lib1中,我无法访问ObjCppWrapper类的iOS类别中的任何方法(在本例中为PreInitialize).我还将两个ObjCppWrapper类都移到了Lib1,即与AppDelegate.swft相同的目标,但应用程序仍然崩溃并出现错误.

不确定为什么找不到PreInitialize方法.而且只支持tvOS.

推荐答案

对于那些寻找准确答案的人(像我),可能错过了问题 comments 中发布的链接.

根本问题是静态库中的Objc类别.有关详细解释,请查看this post.

解决方案是添加,

-对象

AppTarget中的链接器标志->;BuildSettings->;其他链接器标志.

Ios相关问答推荐

如何在iOS中以编程方式推送另一个页面?

如何在iOS 17 SwiftUI中获取用户名

WatchConnectivity - 从手表配套应用程序在 iOS 设备上启动 AVPlayer

SwiftUI - 如何从主题感知命名 colored颜色 中获取正确的 RGB 值?

Flutter firebase_auth 中的无效应用程序凭据/令牌不匹配

UIView 倒置旋转时的zoom 问题

Flutter iOS 构建失败并在 ios/Runner/AppDelegate.swift 中出现多个错误

如何让替换视图淡入旧视图而不是交叉淡入淡出?

如何使用 AppIntents 在 SwiftUI 应用程序中打开特定视图

SwiftUI 选项卡式视图防止滑动到下一个选项卡

Swift 5.7 - 使存在的任何协议符合 Hashable

UIViewRepresentable - MKMapView 没有得到更新

Xcode - 错误 ITMS-90635 - Bundle 包中的 Mach-O 无效 - 提交到 App Store

CocoaPods 找不到 podFirebase/Core的兼容版本 | cloud_firestore, Flutter

'+entityForName: nil 不是合法的 NSManagedObjectContext 参数 - 核心数据

在 iOS 8 中显示相机权限对话框

如何启用滑动以删除 TableView 中的单元格?

iPhone:什么是 WWDR 中级证书?

如何删除UITableView中最后一个单元格的最后一个边框?

我可以在 UIScrollView 中使用 UIRefreshControl 吗?