Check if class conforms to protocol

Peter Segerblom

I am trying to make a simple Dependency Injection system for our app in swift, for 2 day now. I'm flexible to whatever solution but I would like something so I can say "give me a instance of something that conforms to this protocol" and the actual type returned can be whatever as long as it conforms to the said protocol. I have tried a great many thing including generics but managed to figure out that that can not(?) really work so now I'm down to the bare basics, something like this:

protocol AProtocol {

}

class AClass: AProtocol {

} 

class MyDiThing {
    public static func objectConformingTo(aProtocol: Any) -> Any? {
        // And here I want to do something like
        if AClass is aProtocol {
            return AClass()
        }
        return nil
    }
}

// The calling code ..
let aObject = MyDIThing.objectConformingTo(AProtocol)

It's not beautiful, I know, but right now i'm not that picky about performance/bad code as long as it solves the decoupling problem (and preferably can be contained in the MyDIThing class). If this is impossible in swift I'm open to other solutions. I have used similar solutions with objective-c with good success, just having a dictionary with keys being NSStringFromProtocol and values being the class, subscripting the dictionary with the inbound protocol and instantiating the class. Super simple. In swift it feels impossible!

Wain

If you import obj-c then you can do something like you used to.

Otherwise, it's hard because protocols don't exist in the same way. Consider a registration based system for your factory. Each of your classes would register themselves by supplying a function or closure that can be called to return a new instance of that class, and the registration is against a string or some other type of identifier. This is where it would be good to have a protocol type, but in obj-c you were really doing the same thing with a string conversion. You could register against anything that is Equatable to keep things very generic.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Extend a Class that conforms a protocol?

Objective-C Runtime: best way to check if class conforms to protocol?

Swift Property that conforms to a Protocol and Class

iOS11 Swift 4 - how to check if Swift class conforms to protocol defined in Objective-C?

Pharo Smalltalk - How can I check if a message conforms to a protocol defined in another object's Class?

Passing a Class which conforms to a protocol as an argument

How to pass a class type, that conforms to protocol

Custom method on class that conforms to MKAnnotation protocol

Protocol Extension in Swift Where Object Is a Class and conforms to a Protocol

Swift 3, function in protocol that return object of class that conforms to a protocol

Use a parent class init inside of a class protocol that conforms to a class

Variable of type that conforms to protocol and class used in generic function

"Type of a class which conforms to a protocol" as parameter in a method swift

Swift extension of a class ONLY when it conforms to a specific protocol

Swift 4+ instantiating a class variable that conforms to a protocol

Checking if a Swift class conforms to a protocol and implements an optional function?

Swift 3. Cast Any to class which conforms specific protocol

Swift init from unknown class which conforms to protocol

Swift extension that conforms to protocol

Swift: AnyClass variable that conforms to protocol

Declare that delegate conforms to another protocol

How do i cast to a generic type like that: MyType<ConformingX> (ConformingX is a class that conforms to a protocol)

How do you create a base class with a weak delegate that conforms to a generic protocol?

Create object of Objective-C class at runtime in Swift, which conforms to Objective-C protocol

variable that conforms to protocol to accept variables that conform to protocol

how to check if char conforms to charset?

var conforms a protocol which has associateType

Is a blank function conventional in subclass that conforms to custom protocol?

How can I assert a struct conforms to a protocol?