why can I do this without any error:
var testDto = ModelDto(modelId: 1)
testDto.objectId = 2
while I define this:
protocol
The behavior you are seeing on your code sample is called member hiding. Member hiding happens in object oriented languages when new a member is declared with the same name or signature of an inherited one, so by having:
var objectId: Int
in your struct implementation, you are effectively creating a new member called objectId and hiding the property inherited from the protocol.
In order to honor the contract between your struct and your protocol, objectId could be declared as:
let objectId: Int = 1
or
var objectId: Int {
get {
return 1
}
}