Swift是否可以从轻量级通用Objective-c类继承? [英] Is it possible for Swift to inherit from an lightweight generic Objective-c Class?

查看:139
本文介绍了Swift是否可以从轻量级通用Objective-c类继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类,基类在Obj-c中,子类在Swift中。

I have two classes, the base class is in Obj-c and the subclass is in Swift.

Obj-c类使用新的XCode 7的轻量级通用功能定义如下:

The Obj-c class uses new XCode 7's lightweight generic feature defined as follows:

@interface BaseController<T: SFObject *> : UIViewController
@property(nonatomic, strong) T model;
@end

这适用于Obj-c子类。

That works fine for Obj-c subclasses.

现在对于Swift,如果我像往常一样定义泛型类型,我会收到以下错误:

Now for Swift if I define the generic type as I usually do I get the following error:


不能专门化非泛型类型'BaseObjcController'。

Cannot specialize non-generic type 'BaseObjcController'.

我的第二个类定义如下:

My second class is defined as follows:

class SwiftController: BaseObjcController<SFUser> {

}

任何帮助将不胜感激。
谢谢。

Any help would be appreciated. Thanks.

推荐答案

来自与Objective-C API交互


注意

NOTE

除了这些Foundation集合类,Objective-C
Swift忽略了轻量级泛型。使用
轻量级泛型的任何其他类型都被导入到Swift中,好像它们是
unparameterized。

Aside from these Foundation collection classes, Objective-C lightweight generics are ignored by Swift. Any other types using lightweight generics are imported into Swift as if they were unparameterized.

所以这是目前不可能。正如您从
生成的界面助手视图中看到的那样,您的Objective-C类被导入
到Swift为

So this is not possible at present. As you can see from the "Generated Interface" assistant view, your Objective-C class is imported to Swift as

public class BaseController : UIViewController {
    public var model: SFObject!
}

这也在Apple开发者论坛中讨论过:

This was also discussed in the Apple Developer Forum:

  • Objective-C generics not visible from Swift?

随意提交增强错误报告!

Feel free to file an enhancement bug report!

这篇关于Swift是否可以从轻量级通用Objective-c类继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆