【发布时间】:2019-11-27 18:59:36
【问题描述】:
在我的 Kotlin 多平台项目中,我大量使用命名空间。另外,由于我使用的是 MVP,所以我有相似的类名。例如:
com.company.project.usecase1.Model
com.company.project.usecase1.View
com.company.project.usecase1.Presenter
com.company.project.usecase2.Model
com.company.project.usecase2.View
com.company.project.usecase2.Presenter
现在我想在 iOS 应用程序中使用它(用 Swift 编写)。
所以我已将 iOS 目标添加到 build.gradle - 就像 example 中的所有内容。我能够生成 Cocoapod (gradlew podspec) 并使用它 Swift 应用程序。
build.gradle的相关部分:
version = "$rootProject.module_version"
kotlin {
cocoapods {
summary = "App MVP of NotesClientApp"
homepage = "some url"
}
}
但是由于 Swift 没有命名空间并且类名看起来很相似,所以生成的 obj-c 包装器看起来很丑:它在类名中使用人工突变(下划线)来区分名称,例如。
__attribute__((swift_name("Presenter__")))
@protocol App_mvpPresenter__ <App_mvpBasePresenter>
@required
@end;
__attribute__((swift_name("View__")))
@protocol App_mvpView__ <App_mvpBaseView>
@required
- (void)showValidationErrorError:(App_mvpKotlinException *)error __attribute__((swift_name("showValidationError(error:)")));
- (void)showNotesList __attribute__((swift_name("showNotesList()")));
@property NSString *host __attribute__((swift_name("host")));
@property NSString *port __attribute__((swift_name("port")));
@end;
__attribute__((swift_name("Model__")))
@interface App_mvpModel__ : KotlinBase
- (instancetype)initWith_host:(NSString * _Nullable)_host _port:(App_mvpUInt * _Nullable)_port __attribute__((swift_name("init(_host:_port:)"))) __attribute__((objc_designated_initializer));
- (void)updateHost:(NSString *)host port:(uint32_t)port __attribute__((swift_name("update(host:port:)")));
@property (readonly) NSString * _Nullable host __attribute__((swift_name("host")));
@property (readonly) App_mvpUInt * _Nullable port __attribute__((swift_name("port")));
@property id<App_mvpPresenter__> _Nullable presenter __attribute__((swift_name("presenter")));
@end;
__attribute__((objc_subclassing_restricted))
__attribute__((swift_name("PresenterImpl__")))
@interface App_mvpPresenterImpl__ : KotlinBase <App_mvpPresenter__>
- (instancetype)initWithModel:(App_mvpModel__ *)model __attribute__((swift_name("init(model:)"))) __attribute__((objc_designated_initializer));
- (void)attachViewView:(id<App_mvpView__>)view __attribute__((swift_name("attachView(view:)")));
- (void)onModelChanged __attribute__((swift_name("onModelChanged()")));
- (void)onViewChanged __attribute__((swift_name("onViewChanged()")));
- (void)onViewDetached __attribute__((swift_name("onViewDetached()")));
@property (readonly) App_mvpModel__ *model __attribute__((swift_name("model")));
@end;
我想应该有一些调整行为的可能性:添加一些配置文件或注释以进行命名调整。
在 Obj-c/Swift 中重命名与 Kotlin 无关的类的任何其他可能性?我尝试使用 Swift 类型别名,但只收到“类型别名无效重新声明”错误。
有什么想法吗?
PS。我也可以看到模块名称(app-mvp)作为前缀,例如。类名App_mvpView__ - 是否有可能在不更改 Gradle 模块名称的情况下调整生成的框架名称(因为我仍然想使用正确的 JVM 构建工件名称:app-mvp-jvm.jar)?
PPS。我确实理解重命名类以使它们在所有命名空间中唯一会更容易,但无论如何。
【问题讨论】:
-
您是如何解决重复命名问题的?
标签: ios swift cocoapods kotlin-multiplatform kotlin-interop