Swift Protocol get only settable?

前端 未结 6 726
一生所求
一生所求 2020-12-08 00:23

why can I do this without any error:

var testDto = ModelDto(modelId: 1)
testDto.objectId = 2

while I define this:

protocol          


        
6条回答
  •  孤城傲影
    2020-12-08 01:10

    In your class, you create a stored property named objectId. In your protocol, you specify that the property needs a getter – that is its only requirement.

    If you wanted it to be a computer property, like you expect it to, you need to declare objectId with the following:

    var objectId: Int{ return (someNumber) }
    

    Without the closure to compute the value, it is, by default, a stored property.

提交回复
热议问题