the Reverend the Reverend - 1 month ago 7
Swift Question

Conforming to a static function that returns "Self"

When trying to conform to NSItemProviderReading, I get the following error:
enter image description here

The protocol definition for this method is as follows:

public static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> Self


The protocol static function returns type "Self", I've tried to change it to the name of the actual class, but then it won't conform to NSItemProviderReading anymore.

How does one return "Self" ?

Update:
This is what happens when I ask Xcode to fix it:
enter image description here

It appends as! Self, but then shows 2 errors and this warning, it looks confusing cause it seems that it wants to revert back to how it was before, returning the instance of the class in this case NameData

Answer Source

Self in a protocol is a requirement that conformance of the protocol use their own type. So you need to change Self to NameData in the return type of the method when you conform this in your class extension.

extension NameData: NSItemProviderReading {
    static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> NameData {
        return NameData(name: "Test")
    }
}

And one more thing, you need to make your NameData class final.