Код: Выделить всё
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.
Код: Выделить всё
// In Lifecycle library
WindowGroup {
ContentView()
.onOpenURL(perform: { (url: URL) in
// Handle URL with which the app is launched.
})
}
Я подумываю об использовании Категории 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
Код: Выделить всё
// 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
Код: Выделить всё
WindowGroup {
ContentView()
.onOpenURL(perform: { (url: URL) in
URLHandler.handleURL(url)
})
}
Код: Выделить всё
Type 'URLHandler' has no member 'handleURL'
Разве это невозможно с помощью ObjC? Если да, есть ли другой способ?
Edit1: я использую последнюю версию взаимодействия Cpp-Swift, представленную в WWDC 2023, которая использует карту модулей для предоставления Swift типов C++ и ObjC. Я считаю, что сделал это правильно, поскольку ошибка - это не > неопределенный символ: URLHandler
, а метод, недоступный в классе, который я реализовал в категории.
Подробнее здесь: https://stackoverflow.com/questions/781 ... ent-target
Мобильная версия