Is it possible for Swift to inherit from an lightweight generic Objective-c Class?

若如初见. 提交于 2019-12-18 09:19:02

问题


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

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

That works fine for Obj-c subclasses.

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

Cannot specialize non-generic type 'BaseObjcController'.

My second class is defined as follows:

class SwiftController: BaseObjcController<SFUser> {

}

Any help would be appreciated. Thanks.


回答1:


Update: As of Swift 3, Objective-C lightweight generics are imported into Swift. For more information, see

  • SE-0057 Importing Objective-C Lightweight Generics
  • Using Imported Lightweight Generics in Swift

Old answer: From Interacting with Objective-C APIs:

NOTE

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.

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!
}

This was also discussed in the Apple Developer Forum:

  • Objective-C generics not visible from Swift?

Feel free to file an enhancement bug report!



来源:https://stackoverflow.com/questions/32628297/is-it-possible-for-swift-to-inherit-from-an-lightweight-generic-objective-c-clas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!