BlackBox
BlackBox

Reputation: 643

Implement an Objective-C method with light weight generics in Swift

I have to implement this method in a DataSource protocol of an Objective-C library

(nullable id<SomeClass>)someMethod;

I am trying to implement it in my class in Swift, specifically, the AppDelegate, with what I believe keeps equal the signature

extension AppDelegate: LIBDataSource {
    @objc func someMethod<T: SomeClass>() -> T? {
        return nil // temporary
    }
}

The problem is that

Is there a way to implement a generic Obj-C method of a Obj-C protocol in Swift? Or do I have to do a separate Objective-C class to accomplish this?

Upvotes: 0

Views: 358

Answers (1)

JeremyP
JeremyP

Reputation: 86661

The syntax

id<SomeClass>

is nothing to do with lightweight generics, it means "any Objective-C class as long as it conforms the protocol SomeClass". Your method doesn't need to be generic but it does need to return an object that conforms to the SomeClass protocol. It's signature should probably be something like

func someMethod() -> SomeClass?

Upvotes: 2

Related Questions