无法从不同目标识别 ObjC 类别

问题描述 投票:0回答:1

我有一个具有以下层次结构的 iOS SwiftUI 项目:

Module hierarchy:

AppTarget -> depends on Experience
Experience -> depends on Lifecycle
Lifecycle

Content of each module:
Lifecycle 
  -- struct conforming App protocol (entry point)
  -- AppDelegate, SceneDelegate etc.
Experience
  -- URLHandler (and other supporting classes to handle launch parameters (if app is launched by tapping an associated file, quick action etc..) and update the UI.

我在

Lifecycle
库中有一个符合App协议的结构体。它定义了 onOpenURL(perform:) 方法来处理所有基于 URL 的启动(小部件、文件、深层链接、通用链接等)。

// In Lifecycle library

WindowGroup {
    ContentView()
        .onOpenURL(perform: { (url: URL) in
            // Handle URL with which the app is launched.
        })
}

在上面的方法中,我需要调用

Experience
库中的URL处理程序方法。请注意,
Experience
库位于层次结构中
Lifecycle
之上(如上所述),因此我不能简单地直接调用它。

我正在考虑使用 ObjC 类别 来实现这一点。 ObjC 类别是一种向现有类添加方法的方法。在运行时,该类别中的所有方法都将位于一个类下,尽管它们是在不同模块的两个文件中定义的。

Lifecycle
库中,我定义类如下:

// In header file,
@interface  URLHandler : NSObject

// No methods defined here.
// Experience extends this using the concept of
// ObjC category and defines a handler method
// to handle all URL handler launches.

@end

// In .mm file, 
@implementation URLHandler

// Empty

@end

Experience
库中,我扩展了它们,如下所示:

// In header file, extend the base class in Lifecycle library
// by creating a category.
@interface URLHandler (Experience)

+ (void) handleURL:(NSURL *) url;

@end

// In .mm file,
@implementation URLHandler (Experience)

+ (void) handleURL:(NSURL *) url {
    Log([NSString stringWithFormat:@"(ObjC++) url = %@", [url absoluteString]]);

    // Update the UI after 'understanding' the URL.
}

@end

并且,在onOpenURL(perform:)方法中,调用handleURL:方法,

WindowGroup {
    ContentView()
        .onOpenURL(perform: { (url: URL) in
            URLHandler.handleURL(url)
        })
}

根据我的理解,一切似乎都很好......但我收到编译错误

Type 'URLHandler' has no member 'handleURL'

类别不是运行时功能吗?在运行时,handleURL:方法应该是

URLHandler
的一部分。但是我如何在编译时克服这个错误呢?

使用 ObjC 不可能吗?如果可以的话还有其他办法吗?

ios objective-c objective-c-category swift-extensions
1个回答
0
投票

我相信您没有导入

Bridging-Header.h
文件中的类别。然后尝试打开它:

#import "URLHandler+Experience.h"
© www.soinside.com 2019 - 2024. All rights reserved.