objective-c - 如何在 Swift 中调用需要运行时类作为参数的 Objective C 方法

标签 objective-c swift objective-c-runtime

我开始使用 swift 编程大约两周了,我仍然想在我的应用程序中使用一些 Objective C 类。一个例子是从 Swift 调用此方法:

+ (void)transitionFromThisVCClass:(Class<SomeProtocol>)aFromVCClass
                       ToThisClassVCClass:(Class<SomeProtocol>)aToVCClass
                    WithNavigationController:(UINavigationController *)aNav
                                WithDuration:(NSTimeInterval)aDuration

我的问题是我找不到像在 Objective C [SomeClass class] 中那样传递类类型的方法。任何对此的帮助将不胜感激。谢谢

更新

如果我尝试像这样使用 MyClass.self:

ASFSharedViewTransition.addTransitionWithFromViewControllerClass(
     RecommendationListViewController.self,
     toViewControllerClass: RecommendationViewController.self,
     withNavigationController: self.window?.rootViewController,
     withDuration: 0.3)

ASFSharedViewTransition 是一个 objective-c 类,我尝试从 swift 调用它

我收到此错误:

Cannot convert the expression's type '(RecommendationListViewController.Type,
toViewControllerClass: RecommendationViewController.Type,
withNavigationController: $T5??,
withDuration: FloatLiteralConvertible)'
to type 'FloatLiteralConvertible'

最佳答案

您可以使用foo.self,其中foo是类名。例如,MyClass.self

关于objective-c - 如何在 Swift 中调用需要运行时类作为参数的 Objective C 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26697352/

相关文章:

objective-c - Xcode——在项目中寻找死方法

objective-c - 定义为返回 id 的 SEL 如何表示返回 double 的方法?

ios - TableView HeaderSections 隐藏按钮

ios - iOS 是否支持可跨越的字符串?

ios - 基于字典键的数组排序 :value in swift

ios - 带有自动布局的静态 UITableViewCell 的自动高度未在 IB 中更新(但在运行时有效)

ios - 什么是 objc_selector 实现?

Swift 4 Objective-C 运行时和转换为 NSObjectProtocol

ios - 根据键的值从 NSDictionary 的 NSMutableArray 中删除重复项

objective-c - 从 NSMutableArray 中获取最后 x 个对象