简体   繁体   中英

swift properties empty setters and getters

I'm a beginner in Ios development so please forgive me if this sounds like a silly question. I often see properties declaration like this:

var desiredAccuracy: CLLocationAccuracy { get set }

so what does { get set } mean? Is it an empty getter and setter for a variable?

This is how a property requirement is declared in a protocol:

Property requirements are always declared as variable properties, prefixed with the var keyword. Gettable and settable properties are indicated by writing { get set } after their type declaration, and gettable properties are indicated by writing { get }.

Apple Swift documentation

You declared that the variable will have setter and getter both in protocol

protocol Abstract  {
  var desiredAccuracy: Int { get set }
}

//so here  you can implement the abstract with struct or class.
struct  Concrete : Abstract {
  var desiredAccuracy: Int
}

var concrete =  Concrete(desiredAccuracy: 10)
print(concrete.desiredAccuracy)   <--  this is getter 
concrete.desiredAccuracy = 20     <--  this is setter
print(concrete.desiredAccuracy)   <--  this is getter 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM