I’ve got a protocol called DTO.
public protocol DTO {
static func GetDefaultInstance() -> DTO
}
I’ve got a struct that conforms to DTO called Exercise.
public struct Exercise: Codable, Identifiable, DTO {
public var id: String { return ExerciseId }
public var ExerciseId: String
public var ExerciseName: String
public static func GetDefaultInstance() -> Exercise { //error here
return Exercise(ExerciseId: "", ExerciseName: "")
}
}
Since Exercise conforms to DTO why can’t I return Exercise? It is a DTO.
>Solution :
The problem is that the signature in the protocol and the declaration don’t actually match, because while Exercise conforms to DTO, it is not exactly DTO.
If you change the protocol definition to return Self, then the protocol will expect that the function returns the type that is implementing the protocol, and that method signature you have will work.
public protocol DTO {
static func GetDefaultInstance() -> Self
}