What Does "Get" Mean in a Protocol's Property Declaration

What does get mean in a protocol's property declaration?

These are properties for which the classes that adopt the protocol must supply a getter. The protocol does not specify anything about the setter, so classes could supply a computed property instead of a stored one.

For example, a class that adopts Constrainable could satisfy a requirement of having topAnchor by adding

var topAnchor: NSLayoutYAxisAnchor

or by adding

var topAnchor: NSLayoutYAxisAnchor {
...
return ...
}

What does the { get } mean with UINavigationController?

get (means Gettable property)

It means you can get this the variable only. You can not set its value.

Gettable and settable properties are indicated by writing { get set } after their type declaration, and gettable properties are indicated by writing { get }.

Generally you will see this in protocol declaration.

For. eg.

protocol TestProtocol: AnyObject {
var newVar: String { get } // <---Gettable only
var otherVar: Bool { get set } //<----- can be Gettable/settable the value
}

What does declaring { get } with a property imply?

That line expresses the requirement of conforming types to have a variable called category, of type String, with at least a getter.

Such a requirement could be satisfied in a conforming type with a let constant, a var variable, or a computed var.

what is the meaning when a property's type is a protocol in swift?

Property1 is anything that conforms to protocol A. So this could either be another class, a struct, etc. Say you have a protocol Vegetable and a class named Market. You'll want to sell multiple types of Vegetable, however, you want to make sure that the vegetables are for sale. You can do this with protocols.

protocol Vegetable {
var isForSale: Bool { get }
}

// Now let's create some vegetables
class Carrot: Vegetable {
let isForSale = true
}

class Spinach: Vegetable {
let isForSale = false
}

// This is our market.
class Market {
let vegetables: [Vegetable] = [Carrot(), Spinach()]

// Now we can check if the vegetables are for sale, because we know for sure that they conform to Vegetable so must implement this variable.
var forSale: [Vegetable] = {
vegetables.filter { $0.isForSale } // This will return [Spinach()]
}
}

Swift Protocols: Difference between { get } and { get set } with concrete examples?

protocol — is a requirement of some minimal interface of the type implementing it.

  • var name: Type { get } requires type to have property with at least a getter (accessible from outside of the type, not private), i.e. outside code should be able to read value of the property. In the implementing type it could be let name: Type, var name: Type, private(set) var name: Type, fileprivate(set) var name: Type, etc.

  • var name: Type { get set } requires type to have property with both accessible getter and setter, i.e. outside code should be able to read and write to the property. Here only var name: Type would be allowed.

If protocol requires for getter but you also provide a setter — it's not against protocol requirements.
But if protocol requires for both getter and setter — you must provide both, and not having any of them won't be valid implementation.


Your Person class defined both properties as var(with accessible getter and setter) therefore you can change them both. But PersonProtocol haven't required ability to set firstName.

And as @JoakimDanielson shows, if you will use just interface required by protocol you won't be to change the firstName value.

Brief property's getter description in Swift

This form of declaring a property is usually used when declaring a protocol. I've also seen it used in generated interfaces.

In the case of a protocol it denotes that the conforming object should expose a getter for the property however a property with both get and set methods will work.

Swift Protocol get only settable?

As per the official documentation:

The getter and setter requirements can be satisfied by a conforming type in a variety of ways. If a property declaration includes both the get and set keywords, a conforming type can implement it with a stored variable property or a computed property that is both readable and writeable (that is, one that implements both a getter and a setter). However, that property declaration can’t be implemented as a constant property or a read-only computed property. If a property declaration includes only the get keyword, it can be implemented as any kind of property.

Define @property in Protocol

@property can also appear in the declaration of a protocol or category.

Stated in the official apple documentation. So no problem there.

How to define variable that can be set and get in extension of protocol

The simplest way i think is define the variable in the protocol with the getter and setter. Then in your conform object you should declare the variable to conformance.
An Example:

protocol AbstractObject {

var maxProductCount: Int { get set }
}

struct ConformObject: AbstractObject {

var maxProductCount: Int
}

So now you can use your variable in your default implementations

extension AbstractObject {

mutating func addOne() -> Int {
self.maxProductCount += 1
return self.maxProductCount
}
}


Related Topics



Leave a reply



Submit