Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


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:

Feel free to file an enhancement bug report!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...